named-parameters 0.0.11 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -164,6 +164,34 @@ method:
164
164
  `declared_parameters` is also available from the class methods of user defined
165
165
  classes.
166
166
 
167
+ Filtering Arguments
168
+ -------------------
169
+ Sometimes you'll have a `Hash` object that will have a bunch of keys that may
170
+ or may not comply to the parameter declaration of a method, but you may to use
171
+ the same object everywhere without having to figure out what keys are
172
+ applicable to a method call:
173
+
174
+ options = { :x => 1, :y => :z, :a => 'somevalue' }
175
+
176
+ has_named_parameters :foo, :required => [ :x ]
177
+ def foo options = { }
178
+ options.inspect
179
+ end
180
+
181
+ has_named_parameters :bar, :required => [ :y ], :optional => [ :a ]
182
+ def bar options = { }
183
+ options.inspect
184
+ end
185
+
186
+ foo options # => ArgumentError! :y and :a not recognized
187
+ bar options # => ArgumentError! :x not recognized
188
+
189
+ Use the `filter_parameters` method against the options object to make it
190
+ comply to the declaration:
191
+
192
+ foo filter_parameters(options) # => [ :x ]
193
+ bar filter_parameters(options) # => [ :a, :y ]
194
+
167
195
  Permissive Mode
168
196
  ---------------
169
197
  When a method is declared with `has_named_parameters` that method will only
data/RELEASENOTES CHANGED
@@ -1,3 +1,6 @@
1
+ 0.0.12 [Nov 25, 2010]
2
+ - [FEATURE] Added apply_filters method.
3
+
1
4
  0.0.11 [Nov 24, 2010]
2
5
  - [BUGFIX] declared_parameters returns nil if called from an uninstrumented
3
6
  method; it will now return an empty Array.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.11
1
+ 0.0.12
@@ -33,14 +33,15 @@ module NamedParameters
33
33
  # declared in the the `has_named_parameters` clause, or the list specified
34
34
  # in either the `requires` and `recognizes` clause.
35
35
  #
36
- # @returns [Array] list of symbols representing the name of the declared
36
+ # @return [Array<Symbol>] the list of symbols representing the name of the declared
37
37
  # parameters.
38
38
  #
39
39
  def declared_parameters
40
40
  klazz = self.instance_of?(Class) ? self : self.class
41
41
  specs = klazz.send :specs
42
42
 
43
- method = self.instance_of?(Class) ? :"self.#{calling_method}" : calling_method
43
+ caller = block_given? ? yield : calling_method # insane-fucker! :-)
44
+ method = self.instance_of?(Class) ? :"self.#{caller}" : caller
44
45
  spec = specs[klazz.send(:key_for, method)]
45
46
  return [] if spec.nil?
46
47
 
@@ -50,13 +51,44 @@ module NamedParameters
50
51
  [ :required, :optional, :oneof ].map{ |k| spec[k].map(&mapper) }.flatten.sort(&sorter)
51
52
  end
52
53
 
54
+ # Filter out keys from `options` that are not declared as parameter to the
55
+ # method:
56
+ #
57
+ # has_named_parameters :foo, :requires => :x
58
+ # def foo options = { }
59
+ # options.inspect
60
+ # end
61
+ #
62
+ # options = { :x => 1, :y => 2, :z => 3 }
63
+ #
64
+ # # the following will fail because :y and :z is not recognized/declared
65
+ # foo options # => ArgumentError!
66
+ #
67
+ # # the following will not fail because we've applied the filter
68
+ # foo filter_parameters(options) # => [ :x ]
69
+ #
70
+ # @param [Hash] options the options argument to the method.
71
+ #
72
+ # @param [Array<Symbol>] the list of symbols representing the declared
73
+ # parameters used to filter `options`. Optional, the list returned by
74
+ # `declared_parameters` is used by default.
75
+ #
76
+ # @return [Hash] a `Hash` whose keys are limited to what's declared as
77
+ # as parameter to the method.
78
+ #
79
+ def filter_parameters options, filter = nil
80
+ caller = calling_method
81
+ filter ||= declared_parameters{ caller }
82
+ options.reject{ |key, value| !filter.include?(key) }
83
+ end
84
+
53
85
  # returns the name of the current method
54
- def current_method
86
+ def current_method # :nodoc:
55
87
  caller[0][/`([^']*)'/, 1].to_sym
56
88
  end
57
89
 
58
90
  # returns the name of the calling method
59
- def calling_method
91
+ def calling_method # :nodoc:
60
92
  caller[1][/`([^']*)'/, 1].to_sym
61
93
  end
62
94
 
@@ -157,7 +189,7 @@ module NamedParameters
157
189
  # has_named_parameters :'self.new', :required => params, :strict
158
190
  # has_named_parameters :initialize, :required => params, :strict
159
191
  #
160
- # @param [Array] params the lists of parameters. The list is expected
192
+ # @param [Array<Symbol>] params the lists of parameters. The list is expected
161
193
  # to be an `Array` of symbols matching the names of the required
162
194
  # parameters.
163
195
  #
@@ -174,7 +206,7 @@ module NamedParameters
174
206
  # has_named_parameters :'self.new', :optional => params, :strict
175
207
  # has_named_parameters :initialize, :optional => params, :strict
176
208
  #
177
- # @param [Array] params the lists of parameters. The list is expected
209
+ # @param [Array<Symbol>] params the lists of parameters. The list is expected
178
210
  # to be an `Array` of symbols matching the names of the optional
179
211
  # parameters.
180
212
  #
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{named-parameters}
8
- s.version = "0.0.11"
8
+ s.version = "0.0.12"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Juris Galang"]
12
- s.date = %q{2010-11-24}
12
+ s.date = %q{2010-11-25}
13
13
  s.description = %q{This gem simulates named-parameters in Ruby.
14
14
  It's a complement to the common Ruby idiom of using Hash args to emulate
15
15
  the use of named parameters. }
@@ -226,4 +226,24 @@ describe "NamedParameters" do
226
226
  o.parameters.should eql [ :a, :b, :c, :x, :y ]
227
227
  DeclaredParameters.singleton(:w => :w, :a => :a).should eql [ :a, :b, :c, :w, :x, :y, :z ]
228
228
  end
229
+
230
+ it "should not return nil when declared_parameters is called on uninstrumented method" do
231
+ class DeclaredParameters
232
+ has_named_parameters :'self.boogey',
233
+ :required => [ :w ],
234
+ :optional => [ :x, [ :y, 1 ], { :z => 1 } ],
235
+ :oneof => [ :a, :b, :c ]
236
+ def self.boogey opts = { }
237
+ declared_parameters
238
+ end
239
+
240
+ def boogey
241
+ declared_parameters
242
+ end
243
+ end
244
+
245
+ o = DeclaredParameters.new(:x => :x, :y => :y)
246
+ o.boogey.should eql []
247
+ DeclaredParameters.boogey(:w => :w, :a => :a).should eql [ :a, :b, :c, :w, :x, :y, :z ]
248
+ end
229
249
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: named-parameters
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
4
+ hash: 7
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 11
10
- version: 0.0.11
9
+ - 12
10
+ version: 0.0.12
11
11
  platform: ruby
12
12
  authors:
13
13
  - Juris Galang
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-24 00:00:00 -08:00
18
+ date: 2010-11-25 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency