Module: NamedParameters::ClassMethods
- Defined in:
- lib/named-parameters/module.rb
Instance Method Summary collapse
-
#has_named_parameters(method, spec, mode = :strict) {|spec| ... } ⇒ Object
Declares that ‘method` will enforce named parameters behavior as described in `spec`; a method declared with `:required` and/or `:optional` parameters will raise an `ArgumentError` if it is invoked without its required parameters or receives an unrecognized parameter.
-
#recognizes(*params, &block) ⇒ Object
Convenience method, equivalent to declaring:.
-
#requires(*params, &block) ⇒ Object
Convenience method, equivalent to declaring:.
Instance Method Details
#has_named_parameters(method, spec, mode = :strict) {|spec| ... } ⇒ Object
Declares that ‘method` will enforce named parameters behavior as described in `spec`; a method declared with `:required` and/or `:optional` parameters will raise an `ArgumentError` if it is invoked without its required parameters or receives an unrecognized parameter.
Sample usage:
has_named_parameters :point, :required => [ :x, :y ], :optional => :color
217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/named-parameters/module.rb', line 217 def has_named_parameters method, spec, mode = :strict # ensure spec entries are initialized and the proper types [ :required, :optional, :oneof ].each{ |k| spec[k] ||= [] } spec = Hash[ spec.map{ |k, v| v = [ v ] unless v.instance_of? Array v.map!{ |entry| entry.instance_of?(Array) ? Hash[*entry] : entry } [ k, v ] } ] spec[:mode] = mode method_specs[key_for(method)] = spec yield spec if block_given? end |
#recognizes(*params, &block) ⇒ Object
Convenience method, equivalent to declaring:
has_named_parameters :'self.new', :optional => params, :strict
has_named_parameters :initialize, :optional => params, :strict
256 257 258 259 260 261 262 |
# File 'lib/named-parameters/module.rb', line 256 def recognizes *params, &block [ :'self.new', :initialize ].each do |method| spec = method_specs[key_for method] || { } spec.merge!(:optional => params) has_named_parameters method, spec, :strict, &block end end |
#requires(*params, &block) ⇒ Object
Convenience method, equivalent to declaring:
has_named_parameters :'self.new', :required => params, :strict
has_named_parameters :initialize, :required => params, :strict
239 240 241 242 243 244 245 |
# File 'lib/named-parameters/module.rb', line 239 def requires *params, &block [ :'self.new', :initialize ].each do |method| spec = method_specs[key_for method] || { } spec.merge!(:required => params) has_named_parameters method, spec, :strict, &block end end |