Sha256: 94afc960b9dd0fff64ce5c29dab009990b6fc113d226f7fd3e0a89f3f102982e

Contents?: true

Size: 1.95 KB

Versions: 1

Compression:

Stored size: 1.95 KB

Contents

require "reverse_parameters/version"

class ReverseParameters

  # @param input [Proc, Array]
  def initialize(input)
    if input.respond_to?(:to_proc)
      @params = input.to_proc.parameters
    elsif input.respond_to?(:to_ary)
      @params = input.to_ary
    else
      raise ArgumentError.new("Input must be an Array of parameters or a Proc object.")
    end
  end

  # Method parameters are the names listed in the function definition.
  # @return [ReverseParameters::Parameters]
  def parameters
    Parameters.new(params)
  end

  # Method arguments are the real values passed to (and received by) the function.
  # @return [ReverseParameters::Arguments]
  def arguments
    Arguments.new(params)
  end

  private
  attr_reader :params

  class BaseCollection
    include Enumerable

    def initialize(collection)
      @collection = collection.map{|state, name| item_class.new(state: state, name: name)}
    end

    def each(&block)
      @collection.send(:each, &block)
    end

    def to_s
      to_a.join(', ')
    end

    def to_a
      map(&:to_s)
    end

    class Item
      def initialize(name:, state:)
        @name  = name
        @state = state
      end
      attr_reader :name, :state
    end
  end

  class Arguments < BaseCollection;
    class Arg < BaseCollection::Item
      def to_s
        case state
          when :key, :keyreq
            "#{name}: #{name}"
          else
            name
        end.to_s
      end
    end

    def item_class
      Arguments::Arg
    end
  end
  class Parameters < BaseCollection;
    class Param < BaseCollection::Item
      def to_s
        case state
          when :req
            name
          when :rest
            "*#{name}"
          when :keyrest
            "**#{name}"
          when :opt
            "#{name}=nil"
          when :keyreq
            "#{name}:"
          when :key
            "#{name}: nil"
        end.to_s
      end
    end

    def item_class
      Parameters::Param
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
reverse_parameters-0.2.0 lib/reverse_parameters.rb