named-parameters 0.0.19 → 0.0.20

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/RELEASENOTES CHANGED
@@ -1,3 +1,8 @@
1
+ 0.0.20 [Dec 12, 2010]
2
+ - [INTERNAL] Modified implementation for #intercept so that it works for Ruby
3
+ 1.8.6, 1.8.7, and 1.9.2
4
+ - [INTERNAL] Intercepted methods will now be able to entertain blocks (again)
5
+
1
6
  0.0.19 [Dec 11, 2010]
2
7
  - [INTERNAL] Will no longer require "bundler/setup"
3
8
  See: https://github.com/jurisgalang/named-parameters/issues#issue/2
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.19
1
+ 0.0.20
@@ -185,6 +185,16 @@ module NamedParameters
185
185
  end
186
186
 
187
187
  module ClassMethods
188
+ ALIAS_PREFIX = :__intercepted__
189
+
190
+ def aliased name # :nodoc:
191
+ :"#{ALIAS_PREFIX}#{name}"
192
+ end
193
+
194
+ def unaliased name # :nodoc:
195
+ name.gsub(/^#{ALIAS_PREFIX}/, '')
196
+ end
197
+
188
198
  # Declares that `method` will enforce named parameters behavior as
189
199
  # described in `spec`; a method declared with `:required` and/or
190
200
  # `:optional` parameters will raise an `ArgumentError` if it is invoked
@@ -313,11 +323,13 @@ module NamedParameters
313
323
  # add instrumentation for class methods
314
324
  def singleton_method_added name # :nodoc:
315
325
  apply_method_spec :"self.#{name}" do
316
- method = self.metaclass.instance_method name
317
- spec = method_specs[key_for(:"self.#{name}")]
318
- owner = "#{self}::"
326
+ self.metaclass.send :alias_method, aliased(name), name
327
+ #method = self.metaclass.instance_method name
328
+ owner = "#{self}::"
329
+ spec = method_specs[key_for(:"self.#{name}")]
319
330
  metaclass.instance_eval do
320
- intercept method, owner, name, spec
331
+ #intercept method, owner, name, spec
332
+ intercept owner, name, spec
321
333
  end
322
334
  end
323
335
  super
@@ -326,10 +338,12 @@ module NamedParameters
326
338
  # add instrumentation for instance methods
327
339
  def method_added name # :nodoc:
328
340
  apply_method_spec name do
329
- method = instance_method name
330
- spec = method_specs[key_for(name)]
331
- owner = "#{self}#"
332
- intercept method, owner, name, spec
341
+ alias_method aliased(name), name
342
+ #method = instance_method name
343
+ owner = "#{self}#"
344
+ spec = method_specs[key_for(name)]
345
+ #intercept method, owner, name, spec
346
+ intercept owner, name, spec
333
347
  end
334
348
  super
335
349
  end
@@ -344,34 +358,91 @@ module NamedParameters
344
358
  end
345
359
  end
346
360
 
361
+ ## insert parameter validation prior to executing the instrumented method
362
+ #def intercept method, owner, name, spec # :nodoc:
363
+ # fullname = "#{owner}#{name}"
364
+ # define_method name do |*args, &block|
365
+ # # locate the argument representing the named parameters value
366
+ # # for the method invocation
367
+ # params = args.last
368
+ # args << (params = { }) unless params.instance_of? Hash
369
+ #
370
+ # # merge the declared default values for params into the arguments
371
+ # # used when the method is invoked
372
+ # defaults = { }
373
+ # spec.each do |k, v|
374
+ # next if k == :mode
375
+ # v.each{ |entry| defaults.merge! entry if entry.instance_of? Hash }
376
+ # end
377
+ # params = defaults.merge params
378
+ #
379
+ # # validate the parameters against the spec
380
+ # NamedParameters::validate_method_specs fullname, params, spec
381
+ #
382
+ # # inject the updated argument values for params into the arguments
383
+ # # before actually making method invocation
384
+ # args[args.length - 1] = params
385
+ # method.bind(self).call(*args, &block)
386
+ # end
387
+ #end
388
+
347
389
  # insert parameter validation prior to executing the instrumented method
348
- def intercept method, owner, name, spec # :nodoc:
349
- fullname = "#{owner}#{name}"
350
- #define_method name do |*args, &block|
351
- define_method name do |*args|
352
- # locate the argument representing the named parameters value
353
- # for the method invocation
354
- params = args.last
355
- args << (params = { }) unless params.instance_of? Hash
390
+ def intercept owner, name, spec # :nodoc:
391
+ class_eval(<<-CODE, __FILE__, __LINE__)
392
+ def #{name}(*args, &block)
393
+ # compute the fully-qualified name of the method
394
+ # this is used when reporting argument errors
395
+ fullname = "#{owner}#{name}"
396
+
397
+ # locate the argument representing the named parameters value
398
+ # for the method invocation
399
+ params = args.last
400
+ args << (params = { }) unless params.instance_of? Hash
401
+
402
+ # merge the declared default values for params into the arguments
403
+ # used when the method is invoked
404
+ spec = #{spec.inspect}
405
+ defaults = { }
406
+ spec.each do |k, v|
407
+ next if k == :mode
408
+ v.each{ |entry| defaults.merge!(entry) if entry.instance_of? Hash }
409
+ end
410
+ params = defaults.merge(params)
411
+
412
+ # validate the parameters against the spec
413
+ NamedParameters::validate_method_specs fullname, params, spec
356
414
 
357
- # merge the declared default values for params into the arguments
358
- # used when the method is invoked
359
- defaults = { }
360
- spec.each do |k, v|
361
- next if k == :mode
362
- v.each{ |entry| defaults.merge! entry if entry.instance_of? Hash }
415
+ # inject the updated argument values for params into the arguments
416
+ # before actually making method invocation
417
+ args[args.length - 1] = params
418
+ method = :"#{ALIAS_PREFIX}#{name}"
419
+ send method, *args, &block
363
420
  end
364
- params = defaults.merge params
365
-
366
- # validate the parameters against the spec
367
- NamedParameters::validate_method_specs fullname, params, spec
368
-
369
- # inject the updated argument values for params into the arguments
370
- # before actually making method invocation
371
- args[args.length - 1] = params
372
- #method.bind(self).call(*args, &block)
373
- method.bind(self).call(*args)
374
- end
421
+ CODE
422
+
423
+ #define_method name do |*args, &block|
424
+ # # locate the argument representing the named parameters value
425
+ # # for the method invocation
426
+ # params = args.last
427
+ # args << (params = { }) unless params.instance_of? Hash
428
+ #
429
+ # # merge the declared default values for params into the arguments
430
+ # # used when the method is invoked
431
+ # defaults = { }
432
+ # spec.each do |k, v|
433
+ # next if k == :mode
434
+ # v.each{ |entry| defaults.merge! entry if entry.instance_of? Hash }
435
+ # end
436
+ # params = defaults.merge params
437
+ #
438
+ # # validate the parameters against the spec
439
+ # NamedParameters::validate_method_specs fullname, params, spec
440
+ #
441
+ # # inject the updated argument values for params into the arguments
442
+ # # before actually making method invocation
443
+ # args[args.length - 1] = params
444
+ # method.bind(self).call(*args, &block)
445
+ #end
375
446
  end
376
447
 
377
448
  # initialize specs table as needed
@@ -382,7 +453,7 @@ module NamedParameters
382
453
  def key_for method
383
454
  type = method.to_s =~ /^self\./ ? :singleton : :instance
384
455
  name = method.to_s.sub(/^self\./, '')
385
- :"#{self}::#{type}.#{name}"
456
+ :"#{self}::#{type}.#{unaliased name}"
386
457
  end
387
458
 
388
459
  # check if in the process of instrumenting a method
@@ -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.19"
8
+ s.version = "0.0.20"
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-12-11}
12
+ s.date = %q{2010-12-12}
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. }
@@ -203,7 +203,7 @@ describe "NamedParameters" do
203
203
  lambda { Required.new :x => :x, :y => :y }.should_not raise_error
204
204
  end
205
205
 
206
- it "should be able to list of recognized parameters" do
206
+ it "should be able to get the list of recognized parameters" do
207
207
  class DeclaredParameters
208
208
  requires :x, :y
209
209
  recognizes :a, :b, :c
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 19
9
- version: 0.0.19
8
+ - 20
9
+ version: 0.0.20
10
10
  platform: ruby
11
11
  authors:
12
12
  - Juris Galang
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-12-11 00:00:00 -08:00
17
+ date: 2010-12-12 00:00:00 -08:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -128,7 +128,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
128
128
  requirements:
129
129
  - - ">="
130
130
  - !ruby/object:Gem::Version
131
- hash: 2045388278523135009
131
+ hash: -1934925740006837865
132
132
  segments:
133
133
  - 0
134
134
  version: "0"