Module: NamedParameters
- Included in:
- Object
- Defined in:
- lib/named-parameters/module.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#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.
-
#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.
-
#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.
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 = { }
puts declared_parameters.inspect
end
foo :x => 1, :y => 2 # => [ :x, :y ]
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 = { }
# ...
end
def
puts declared_parameters_for(:foo).inspect
end
# => [ :x, :y ]
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 = { }
.inspect
end
# the following will fail because :y and :z is not recognized/declared
= { :x => 1, :y => 2, :z => 3 }
foo # => ArgumentError!
# the following will not fail because we've applied the filter
foo filter_parameters() # => [ :x ]
114 115 116 117 118 119 |
# File 'lib/named-parameters/module.rb', line 114 def filter_parameters , filter = nil caller = __calling_method method = self.instance_of?(Class) ? :"self.#{caller}" : caller filter ||= declared_parameters { method } .reject{ |key, value| !filter.include?(key) } end |