named-parameters 0.0.13 → 0.0.14

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -158,12 +158,70 @@ method:
158
158
  end
159
159
 
160
160
  # create an instance of GoogleStorage
161
- # and print: [ access-key, secret-key, group-email, apps-domain ]
161
+ # and print: [ :access-key, :secret-key, :group-email, :apps-domain ]
162
162
  GoogleStorage.new :'access-key' => '...', :'secret-key' => '...'
163
163
 
164
164
  `declared_parameters` is also available from the class methods of user defined
165
165
  classes.
166
166
 
167
+ You can also pass a list of parameter types to limit the result to specific
168
+ parameter types:
169
+
170
+ class GoogleStorage
171
+ requires :'access-key', :'secret-key'
172
+ recognizes [ :'group-email', 'group@example.org' ], [ :'apps-domain', 'example.org' ]
173
+
174
+ def initialize options
175
+ # list the parameters declared
176
+ puts "#{declared_parameters(:required).join(' ')}"
177
+
178
+ # ... now do the googly stuff ...
179
+ end
180
+ end
181
+
182
+ # create an instance of GoogleStorage
183
+ # and print: [ :access-key, :secret-key ]
184
+ GoogleStorage.new :'access-key' => '...', :'secret-key' => '...'
185
+
186
+ The method `declared_parameters` is context specific. It returns the list of
187
+ parameters for the current method. To get a list of parameters for a specific
188
+ method, use `declared_parameters_for`:
189
+
190
+ class GoogleStorage
191
+ requires :'access-key', :'secret-key'
192
+ recognizes [ :'group-email', 'group@example.org' ], [ :'apps-domain', 'example.org' ]
193
+
194
+ def initialize options
195
+ # list the parameters declared
196
+ puts "#{declared_parameters(:required).join(' ')}"
197
+
198
+ # ... now do the googly stuff ...
199
+ end
200
+
201
+ def self.required_parameters
202
+ declared_parameters_for :new, :required
203
+ end
204
+
205
+ def self.optional_parameters
206
+ declared_parameters_for :new, :optional
207
+ end
208
+
209
+ def self.all_parameters
210
+ declared_parameters_for :new
211
+ end
212
+ end
213
+
214
+ # list the required parameters for the class
215
+ GoogleStorage.required_parameters # => [ :access-key, :secret-key ]
216
+
217
+ # list the optional parameters for the class
218
+ GoogleStorage.required_parameters # => [ :group-email, :apps-domain ]
219
+
220
+ # list all of the recognized parameters for the class
221
+ GoogleStorage.all_parameters # => [ :access-key, :secret-key, :group-email, :apps-domain ]
222
+
223
+ Notice that both methods may receive a filter of parameter types.
224
+
167
225
  Filtering Arguments
168
226
  -------------------
169
227
  Sometimes you'll have a `Hash` object that will have a bunch of keys that may
data/RELEASENOTES CHANGED
@@ -1,3 +1,16 @@
1
+ 0.0.14 [Nov 29, 2010]
2
+ - [FEATURE] Added declared_parameters_for method.
3
+ - [FEATURE] You can now limit/filter the list of declared parameters based on
4
+ type:
5
+
6
+ has_named_parameters :foobar,
7
+ :required => :a, :optional => [ :b, :c ]
8
+ def foobar
9
+ declared_parameters(:required).inspect
10
+ end
11
+
12
+ foobar # => [ :a ]
13
+
1
14
  0.0.13 [Nov 28, 2010]
2
15
  - [INTERNAL] instrument -> apply_method_spec, to make it compatible with Rails.
3
16
 
@@ -18,7 +31,7 @@
18
31
  declared_parameters.inspect
19
32
  end
20
33
 
21
- foobar # => [ :a, :b, :c ]
34
+ foobar # => [ :a, :b, :c ]
22
35
 
23
36
  - [INTERNAL] Parameter spec table is now retained.
24
37
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.13
1
+ 0.0.14
@@ -33,10 +33,13 @@ 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
+ # @param [Array<Symbol>] type limits the list of parameters returned to the
37
+ # parameter types specified. Defaults to `[ :required, :optional, :oneof ]`
38
+ #
36
39
  # @return [Array<Symbol>] the list of symbols representing the name of the declared
37
40
  # parameters.
38
41
  #
39
- def declared_parameters
42
+ def declared_parameters type = [ :required, :optional, :oneof ]
40
43
  klazz = self.instance_of?(Class) ? self : self.class
41
44
  specs = klazz.send :specs
42
45
 
@@ -48,7 +51,11 @@ module NamedParameters
48
51
  mapper = lambda{ |entry| entry.instance_of?(Hash) ? entry.keys.first : entry }
49
52
  sorter = lambda{ |x, y| x.to_s <=> y.to_s }
50
53
 
51
- [ :required, :optional, :oneof ].map{ |k| spec[k].map(&mapper) }.flatten.sort(&sorter)
54
+ Array(type).map{ |k| spec[k].map(&mapper) }.flatten.sort(&sorter)
55
+ end
56
+
57
+ def declared_parameters_for method, type = [ :required, :optional, :oneof ]
58
+ declared_parameters(type) { method }
52
59
  end
53
60
 
54
61
  # Filter out keys from `options` that are not declared as parameter to the
@@ -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.13"
8
+ s.version = "0.0.14"
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-28}
12
+ s.date = %q{2010-11-29}
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. }
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: 5
4
+ hash: 3
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 13
10
- version: 0.0.13
9
+ - 14
10
+ version: 0.0.14
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-28 00:00:00 -08:00
18
+ date: 2010-11-29 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency