Module: NamedParameters

Included in:
Object
Defined in:
lib/named-parameters/module.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#declared_parameters(type = [ :required, :optional, :oneof ]) ⇒ Array<Symbol>

Returns the list of declared parameters for the calling method, ie: the concatenation of `:required`, `:optional`, and `:oneof` parameter list as declared in the the `has_named_parameters` clause, or the list specified in either the `requires` and `recognizes` clause.

has_named_parameters :foo, :required => [ :x ], :optional => [ :y ]
def foo options = { }
  puts declared_parameters.inspect
end

foo :x => 1, :y => 2   # => [ :x, :y ]

Parameters:

  • type (Array<Symbol>) (defaults to: [ :required, :optional, :oneof ])

    limits the list of parameters returned to the parameter types specified.

Returns:

  • (Array<Symbol>)

    the list of symbols representing the name of the declared parameters.


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/named-parameters/module.rb', line 45

def declared_parameters type = [ :required, :optional, :oneof ]
  klazz  = self.instance_of?(Class) ? self : self.class
  specs  = klazz.send :__method_specs
  method = if block_given?
    yield # insane-fucker! :-)
  else
    caller = __calling_method
    self.instance_of?(Class) ? :"self.#{caller}" : caller
  end

  return [] unless spec = specs[klazz.send(:__key_for, method)]

  mapper = lambda{ |entry| entry.instance_of?(Hash) ? entry.keys.first : entry }
  sorter = lambda{ |x, y| x.to_s <=> y.to_s }
  Array(type).map{ |k| spec[k].map(&mapper) }.flatten.sort(&sorter)
end

#declared_parameters_for(method, type = [ :required, :optional, :oneof ]) ⇒ Array<Symbol>

Returns the list of declared parameters for a specific method, ie: the concatenation of `:required`, `:optional`, and `:oneof` parameter list as declared in the the #has_named_parameters clause, or the list specified in either the `requires` and `recognizes` clause.

has_named_parameters :foo, :required => [ :x ], :optional => [ :y ]
def foo options = { }
  # ...
end

def bar
  puts declared_parameters_for(:foo).inspect
end

bar   # => [ :x, :y ]

Parameters:

  • method (Symbol)

    the name of the method in question.

  • type (Array<Symbol>) (defaults to: [ :required, :optional, :oneof ])

    limits the list of parameters returned to the parameter types specified.

Returns:

  • (Array<Symbol>)

    the list of symbols representing the name of the declared parameters.


86
87
88
# File 'lib/named-parameters/module.rb', line 86

def declared_parameters_for method, type = [ :required, :optional, :oneof ]
  declared_parameters(type) { method }
end

#filter_parameters(options, filter = nil) ⇒ Hash

Filter out keys from `options` that are not declared as parameter to the method:

has_named_parameters :foo, :required => :x
def foo options = { }
  options.inspect
end

# the following will fail because :y and :z is not recognized/declared
options = { :x => 1, :y => 2, :z => 3 } 
foo options   # => ArgumentError! 

# the following will not fail because we've applied the filter
foo filter_parameters(options)   # => [ :x ]

Parameters:

  • options (Hash)

    the options argument to the method.

  • filter (Array<Symbol>) (defaults to: nil)

    the list of symbols representing the declared parameters used to filter `options`. If not specified then the list returned by #declared_parameters is used by default.

Returns:

  • (Hash)

    a `Hash` whose keys are limited to what's declared as as parameter to the method.


114
115
116
117
118
119
# File 'lib/named-parameters/module.rb', line 114

def filter_parameters options, filter = nil
  caller = __calling_method
  method = self.instance_of?(Class) ? :"self.#{caller}" : caller
  filter ||= declared_parameters { method } 
  options.reject{ |key, value| !filter.include?(key) }
end