Sha256: b8fb133f9053cee4762c280a0ab51972977f939c03778796113e8a06217c48fe

Contents?: true

Size: 1.78 KB

Versions: 1

Compression:

Stored size: 1.78 KB

Contents

require "reverse_parameters/version"

class ReverseParameters

  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.
  def parameters
    Parameters.new(params)
  end

  # Method arguments are the real values passed to (and received by) the function.
  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 :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.1.0 lib/reverse_parameters.rb