named-parameters 0.0.8 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/README.md CHANGED
@@ -140,6 +140,29 @@ in a class definition:
140
140
  has_named_parameters :'self.new', :optional => params, :strict
141
141
  has_named_parameters :initialize, :optional => params, :strict
142
142
 
143
+ What Was Declared?
144
+ ------------------
145
+ You can get a list of declared parameters for a method by invoking
146
+ `recognized_parameters`:
147
+
148
+ class GoogleStorage
149
+ requires :'access-key', :'secret-key'
150
+ recognizes [ :'group-email', 'group@example.org' ], [ :'apps-domain', 'example.org' ]
151
+
152
+ def initialize options
153
+ # list the parameters declared
154
+ puts "#{recognized_parameters.join(' ')}"
155
+
156
+ # ... now do the googly stuff ...
157
+ end
158
+ end
159
+
160
+ # create an instance of GoogleStorage
161
+ # and print: [ access-key, secret-key, group-email, apps-domain ]
162
+ GoogleStorage.new :'access-key' => '...', :'secret-key' => '...'
163
+
164
+ `recognized_parameters` is also accessible from singleton methods.
165
+
143
166
  Permissive Mode
144
167
  ---------------
145
168
  When a method is declared with `has_named_parameters` that method will only
data/RELEASENOTES CHANGED
@@ -1,3 +1,23 @@
1
+ 0.0.9 [Nov 24, 2010]
2
+ - [FEATURE] You can now query what parameters have been declared for a method:
3
+
4
+ class Foo
5
+ has_named_parameters :bar,
6
+ :required => :x, :optional => [ :y, :z ]
7
+ def bar options = { }
8
+ required_parameters.inspect
9
+ end
10
+
11
+ has_named_parameters :'self.zoo',
12
+ :required => :a, :optional => [ :b, :c ]
13
+ def self.zoo
14
+ required_parameters.inspect
15
+ end
16
+ end
17
+
18
+ Foo.new.bar # => [ :x, :y, :z ]
19
+ Foo.zoo # => [ :a, :b, :c ]
20
+
1
21
  0.0.8 [Nov 22, 2010]
2
22
  - [INTERNAL] Parameter spec will no longer be shared between singleton and
3
23
  instance methods. Bonus performance improvement: it no longer needs to keep
data/Rakefile CHANGED
@@ -6,16 +6,9 @@ begin
6
6
  Jeweler::Tasks.new do |gem|
7
7
  gem.name = "named-parameters"
8
8
  gem.summary = %Q{Poor man's named-parameters in Ruby}
9
- gem.description = %Q{This gem simulates named-parameters in Ruby. It's a complement to the common
10
- Ruby idiom of using `Hash` args to emulate the use of named parameters.
11
-
12
- It does this by extending the language with a `has_named_parameters` clause
13
- that allows a class to declare the parameters that are acceptable to a method.
14
-
15
- The `has_named_parameters` dictates how the presence of these parameters are
16
- enforced and raises an `ArgumentError` when a method invocation is made that
17
- violates the rules for those parameters.
18
- }
9
+ gem.description = %Q{This gem simulates named-parameters in Ruby.
10
+ It's a complement to the common Ruby idiom of using Hash args to emulate
11
+ the use of named parameters. }
19
12
  gem.email = "jurisgalang@gmail.com"
20
13
  gem.homepage = "http://github.com/jurisgalang/named-parameters"
21
14
  gem.authors = ["Juris Galang"]
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.8
1
+ 0.0.9
@@ -28,6 +28,36 @@ module NamedParameters
28
28
  end
29
29
 
30
30
  private
31
+ # Returns the list of declared parameters for the calling method, ie: the
32
+ # concatenation `:required`, `:optional`, and `:oneof` parameter list as
33
+ # declared in the the `has_named_parameters` clause, or the list specified
34
+ # in either the `requires` and `recognizes` clause.
35
+ #
36
+ # @returns [Array] list of symbols representing the name of the declared
37
+ # parameters.
38
+ #
39
+ def recognized_parameters
40
+ klazz = self.instance_of?(Class) ? self : self.class
41
+ specs = klazz.send :specs
42
+
43
+ method = self.instance_of?(Class) ? :"self.#{calling_method}" : calling_method
44
+ spec = specs[klazz.send(:key_for, method)]
45
+
46
+ mapper = lambda{ |entry| entry.instance_of?(Hash) ? entry.keys.first : entry }
47
+ sorter = lambda{ |x, y| x.to_s <=> y.to_s }
48
+ [ :required, :optional, :oneof ].map{ |k| spec[k].map(&mapper) }.flatten.sort(&sorter)
49
+ end
50
+
51
+ # returns the name of the current method
52
+ def current_method
53
+ caller[0][/`([^']*)'/, 1].to_sym
54
+ end
55
+
56
+ # returns the name of the calling method
57
+ def calling_method
58
+ caller[1][/`([^']*)'/, 1].to_sym
59
+ end
60
+
31
61
  # this is the method used to validate the name of the received parameters
32
62
  # (based on the defined spec) when an instrumented method is invoked.
33
63
  #
@@ -39,14 +69,14 @@ module NamedParameters
39
69
 
40
70
  # determine what keys are allowed, unless mode is :permissive
41
71
  # in which case we don't care unless its listed as required or oneof
42
- order = lambda{ |x, y| x.to_s <=> y.to_s }
43
- allowed = spec[:mode] == :permissive ? [] : (optional + required + oneof).sort(&order)
72
+ sorter = lambda{ |x, y| x.to_s <=> y.to_s }
73
+ allowed = spec[:mode] == :permissive ? [] : (optional + required + oneof).sort(&sorter)
44
74
 
45
75
  # determine what keys were passed;
46
76
  # also, plugin the names of parameters assigned with default values
47
77
  keys = params.keys.map{ |k| k.to_sym }
48
- keys.sort! &order
49
- required.sort! &order
78
+ keys.sort! &sorter
79
+ required.sort! &sorter
50
80
 
51
81
  # this lambda is used to present the list of parameters as a string
52
82
  list = lambda{ |params| params.join(", ") }
@@ -77,7 +107,7 @@ module NamedParameters
77
107
  "Unrecognized parameter specified on call to #{name}: #{list[k]}" \
78
108
  unless k.empty?
79
109
  end
80
-
110
+
81
111
  module ClassMethods
82
112
  # Declares that `method` will enforce named parameters behavior as
83
113
  # described in `spec`; a method declared with `:required` and/or
@@ -191,7 +221,7 @@ module NamedParameters
191
221
  def singleton_method_added name # :nodoc:
192
222
  instrument :"self.#{name}" do
193
223
  method = self.eigenclass.instance_method name
194
- spec = specs.delete(key_for :"self.#{name}")
224
+ spec = specs[key_for :"self.#{name}"]
195
225
  owner = "#{self.name}::"
196
226
  eigenclass.instance_eval do
197
227
  intercept method, owner, name, spec
@@ -204,7 +234,7 @@ module NamedParameters
204
234
  def method_added name # :nodoc:
205
235
  instrument name do
206
236
  method = instance_method name
207
- spec = specs.delete(key_for name)
237
+ spec = specs[key_for name]
208
238
  owner = "#{self.name}#"
209
239
  intercept method, owner, name, spec
210
240
  end
@@ -256,7 +286,8 @@ module NamedParameters
256
286
 
257
287
  def key_for method
258
288
  type = method.to_s =~ /^self\./ ? :singleton : :instance
259
- :"#{self.name}::#{type}.#{method}"
289
+ name = method.to_s.sub(/^self\./, '')
290
+ :"#{self.name}::#{type}.#{name}"
260
291
  end
261
292
 
262
293
  # check if in the process of instrumenting a method
@@ -3,4 +3,6 @@ require 'named-parameters/module'
3
3
 
4
4
  # Extend object to automatically make it available for all
5
5
  # user defined classes...
6
- Object.extend NamedParameters::ClassMethods
6
+ class Object
7
+ include NamedParameters
8
+ end
@@ -1,52 +1,47 @@
1
1
  # Generated by jeweler
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{named-parameters}
8
- s.version = "0.0.8"
8
+ s.version = "0.0.9"
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-22}
13
- s.description = %q{This gem simulates named-parameters in Ruby. It's a complement to the common
14
- Ruby idiom of using `Hash` args to emulate the use of named parameters.
15
-
16
- It does this by extending the language with a `has_named_parameters` clause
17
- that allows a class to declare the parameters that are acceptable to a method.
18
-
19
- The `has_named_parameters` dictates how the presence of these parameters are
20
- enforced and raises an `ArgumentError` when a method invocation is made that
21
- violates the rules for those parameters.
22
- }
12
+ s.date = %q{2010-11-24}
13
+ s.description = %q{This gem simulates named-parameters in Ruby.
14
+ It's a complement to the common Ruby idiom of using Hash args to emulate
15
+ the use of named parameters. }
23
16
  s.email = %q{jurisgalang@gmail.com}
24
17
  s.extra_rdoc_files = [
25
18
  "README.md"
26
19
  ]
27
20
  s.files = [
28
21
  ".document",
29
- "GPL-LICENSE",
30
- "MIT-LICENSE",
31
- "README.md",
32
- "RELEASENOTES",
33
- "Rakefile",
34
- "VERSION",
35
- "lib/named-parameters.rb",
36
- "lib/named-parameters/module.rb",
37
- "lib/named-parameters/object.rb",
38
- "named-parameters.gemspec",
39
- "spec/named-parameters_spec.rb",
40
- "spec/spec.opts",
41
- "spec/spec_helper.rb"
22
+ ".gitignore",
23
+ "GPL-LICENSE",
24
+ "MIT-LICENSE",
25
+ "README.md",
26
+ "RELEASENOTES",
27
+ "Rakefile",
28
+ "VERSION",
29
+ "lib/named-parameters.rb",
30
+ "lib/named-parameters/module.rb",
31
+ "lib/named-parameters/object.rb",
32
+ "named-parameters.gemspec",
33
+ "spec/named-parameters_spec.rb",
34
+ "spec/spec.opts",
35
+ "spec/spec_helper.rb"
42
36
  ]
43
37
  s.homepage = %q{http://github.com/jurisgalang/named-parameters}
38
+ s.rdoc_options = ["--charset=UTF-8"]
44
39
  s.require_paths = ["lib"]
45
40
  s.rubygems_version = %q{1.3.7}
46
41
  s.summary = %q{Poor man's named-parameters in Ruby}
47
42
  s.test_files = [
48
43
  "spec/named-parameters_spec.rb",
49
- "spec/spec_helper.rb"
44
+ "spec/spec_helper.rb"
50
45
  ]
51
46
 
52
47
  if s.respond_to? :specification_version then
@@ -202,4 +202,28 @@ describe "NamedParameters" do
202
202
  lambda { Required.new :y => :y }.should raise_error ArgumentError
203
203
  lambda { Required.new :x => :x, :y => :y }.should_not raise_error
204
204
  end
205
+
206
+ it "should be able to list of recognized parameters" do
207
+ class RecognizedParameters
208
+ requires :x, :y
209
+ recognizes :a, :b, :c
210
+ attr :parameters
211
+
212
+ def initialize opts = { }
213
+ @parameters = recognized_parameters
214
+ end
215
+
216
+ has_named_parameters :'self.singleton',
217
+ :required => [ :w ],
218
+ :optional => [ :x, [ :y, 1 ], { :z => 1 } ],
219
+ :oneof => [ :a, :b, :c ]
220
+ def self.singleton opts = { }
221
+ recognized_parameters
222
+ end
223
+ end
224
+
225
+ o = RecognizedParameters.new(:x => :x, :y => :y)
226
+ o.parameters.should eql [ :a, :b, :c, :x, :y ]
227
+ RecognizedParameters.singleton(:w => :w, :a => :a).should eql [ :a, :b, :c, :w, :x, :y, :z ]
228
+ end
205
229
  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: 15
4
+ hash: 13
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 8
10
- version: 0.0.8
9
+ - 9
10
+ version: 0.0.9
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-22 00:00:00 -08:00
18
+ date: 2010-11-24 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -48,7 +48,7 @@ dependencies:
48
48
  version: "0"
49
49
  type: :development
50
50
  version_requirements: *id002
51
- description: "This gem simulates named-parameters in Ruby. It's a complement to the common \n Ruby idiom of using `Hash` args to emulate the use of named parameters. \n\n It does this by extending the language with a `has_named_parameters` clause \n that allows a class to declare the parameters that are acceptable to a method.\n\n The `has_named_parameters` dictates how the presence of these parameters are\n enforced and raises an `ArgumentError` when a method invocation is made that\n violates the rules for those parameters.\n "
51
+ description: "This gem simulates named-parameters in Ruby. \n It's a complement to the common Ruby idiom of using Hash args to emulate \n the use of named parameters. "
52
52
  email: jurisgalang@gmail.com
53
53
  executables: []
54
54
 
@@ -58,6 +58,7 @@ extra_rdoc_files:
58
58
  - README.md
59
59
  files:
60
60
  - .document
61
+ - .gitignore
61
62
  - GPL-LICENSE
62
63
  - MIT-LICENSE
63
64
  - README.md
@@ -76,8 +77,8 @@ homepage: http://github.com/jurisgalang/named-parameters
76
77
  licenses: []
77
78
 
78
79
  post_install_message:
79
- rdoc_options: []
80
-
80
+ rdoc_options:
81
+ - --charset=UTF-8
81
82
  require_paths:
82
83
  - lib
83
84
  required_ruby_version: !ruby/object:Gem::Requirement