named-parameters 0.0.20 → 0.0.21
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 +6 -0
- data/VERSION +1 -1
- data/lib/named-parameters/module.rb +57 -105
- data/named-parameters.gemspec +10 -4
- data/spec/has_named_parameters_spec.rb +19 -0
- data/spec/{named-parameters_spec.rb → legacy_spec.rb} +1 -1
- data/spec/recognizes_spec.rb +160 -0
- data/spec/requires_spec.rb +73 -0
- metadata +12 -6
data/RELEASENOTES
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
0.0.21 [Dec 16, 2010]
|
2
|
+
- [INTERNAL] Improved syntax handling
|
3
|
+
- [INTERNAL] Reorganized tests
|
4
|
+
- [INTERNAL] Support added for multi-line requires and recognizes clause
|
5
|
+
- [INTERNAL] "Safe" names for privates
|
6
|
+
|
1
7
|
0.0.20 [Dec 12, 2010]
|
2
8
|
- [INTERNAL] Modified implementation for #intercept so that it works for Ruby
|
3
9
|
1.8.6, 1.8.7, and 1.9.2
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.21
|
@@ -44,15 +44,15 @@ module NamedParameters
|
|
44
44
|
#
|
45
45
|
def declared_parameters type = [ :required, :optional, :oneof ]
|
46
46
|
klazz = self.instance_of?(Class) ? self : self.class
|
47
|
-
specs = klazz.send :
|
47
|
+
specs = klazz.send :__method_specs
|
48
48
|
method = if block_given?
|
49
49
|
yield # insane-fucker! :-)
|
50
50
|
else
|
51
|
-
caller =
|
51
|
+
caller = __calling_method
|
52
52
|
self.instance_of?(Class) ? :"self.#{caller}" : caller
|
53
53
|
end
|
54
54
|
|
55
|
-
return [] unless spec = specs[klazz.send(:
|
55
|
+
return [] unless spec = specs[klazz.send(:__key_for, method)]
|
56
56
|
|
57
57
|
mapper = lambda{ |entry| entry.instance_of?(Hash) ? entry.keys.first : entry }
|
58
58
|
sorter = lambda{ |x, y| x.to_s <=> y.to_s }
|
@@ -112,7 +112,7 @@ module NamedParameters
|
|
112
112
|
# as parameter to the method.
|
113
113
|
#
|
114
114
|
def filter_parameters options, filter = nil
|
115
|
-
caller =
|
115
|
+
caller = __calling_method
|
116
116
|
method = self.instance_of?(Class) ? :"self.#{caller}" : caller
|
117
117
|
filter ||= declared_parameters { method }
|
118
118
|
options.reject{ |key, value| !filter.include?(key) }
|
@@ -125,12 +125,12 @@ module NamedParameters
|
|
125
125
|
|
126
126
|
private
|
127
127
|
# returns the name of the current method
|
128
|
-
def
|
128
|
+
def __current_method # :nodoc:
|
129
129
|
caller[0][/`([^']*)'/, 1].to_sym
|
130
130
|
end
|
131
131
|
|
132
132
|
# returns the name of the calling method
|
133
|
-
def
|
133
|
+
def __calling_method # :nodoc:
|
134
134
|
caller[1][/`([^']*)'/, 1].to_sym
|
135
135
|
end
|
136
136
|
|
@@ -138,10 +138,10 @@ module NamedParameters
|
|
138
138
|
# (based on the defined spec) when an instrumented method is invoked.
|
139
139
|
#
|
140
140
|
def self.validate_method_specs name, params, spec # :nodoc:
|
141
|
-
mapper = lambda{ |n| n.instance_of?(Hash) ? n.keys
|
142
|
-
optional = spec[:optional].map
|
143
|
-
required = spec[:required].map
|
144
|
-
oneof = spec[:oneof].map
|
141
|
+
mapper = lambda{ |n| n.instance_of?(Hash) ? n.keys : n }
|
142
|
+
optional = spec[:optional].map(&mapper).flatten
|
143
|
+
required = spec[:required].map(&mapper).flatten
|
144
|
+
oneof = spec[:oneof].map(&mapper).flatten
|
145
145
|
|
146
146
|
# determine what keys are allowed, unless mode is :permissive
|
147
147
|
# in which case we don't care unless its listed as required or oneof
|
@@ -187,14 +187,6 @@ module NamedParameters
|
|
187
187
|
module ClassMethods
|
188
188
|
ALIAS_PREFIX = :__intercepted__
|
189
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
|
-
|
198
190
|
# Declares that `method` will enforce named parameters behavior as
|
199
191
|
# described in `spec`; a method declared with `:required` and/or
|
200
192
|
# `:optional` parameters will raise an `ArgumentError` if it is invoked
|
@@ -236,20 +228,22 @@ module NamedParameters
|
|
236
228
|
# v.map!{ |entry| entry.instance_of?(Array) ? Hash[*entry] : entry }
|
237
229
|
# [ k, v ]
|
238
230
|
# } ]
|
239
|
-
#
|
231
|
+
#
|
240
232
|
# but we have to play nice with ruby 1.8.6, so we'll have to be content
|
241
233
|
# with the ugliness for now...
|
234
|
+
#
|
242
235
|
pairs = spec.map{ |k, v|
|
243
236
|
v = [ v ] unless v.instance_of? Array
|
244
|
-
v.map!{ |entry| entry.instance_of?(Array) ? Hash[*entry] : entry
|
237
|
+
v.map!{ |entry| entry.instance_of?(Array) ? Hash[*entry] : entry }
|
245
238
|
[ k, v ]
|
246
239
|
}
|
240
|
+
|
247
241
|
spec = { }
|
248
242
|
pairs.each{ |x| spec[x[0]] = x[1] }
|
249
243
|
spec = Hash[ spec ]
|
250
244
|
|
251
245
|
spec[:mode] = mode
|
252
|
-
|
246
|
+
__method_specs[__key_for(method)] = spec
|
253
247
|
yield spec if block_given?
|
254
248
|
end
|
255
249
|
|
@@ -263,9 +257,12 @@ module NamedParameters
|
|
263
257
|
# parameters.
|
264
258
|
#
|
265
259
|
def requires *params, &block
|
260
|
+
#raise ArgumentError, \
|
261
|
+
# "You must specify at least one parameter when declaring a requiring clause" \
|
262
|
+
# if params.empty?
|
266
263
|
[ :'self.new', :initialize ].each do |method|
|
267
|
-
spec =
|
268
|
-
spec.merge!(:required => params)
|
264
|
+
spec = __method_specs[__key_for(method)] || { }
|
265
|
+
spec.merge!(:required => Array(spec[:required]) + params)
|
269
266
|
has_named_parameters method, spec, :strict, &block
|
270
267
|
end
|
271
268
|
end
|
@@ -280,9 +277,12 @@ module NamedParameters
|
|
280
277
|
# parameters.
|
281
278
|
#
|
282
279
|
def recognizes *params, &block
|
280
|
+
#raise ArgumentError, \
|
281
|
+
# "You must specify at least one parameter when declaring a recognizes clause" \
|
282
|
+
# if params.empty?
|
283
283
|
[ :'self.new', :initialize ].each do |method|
|
284
|
-
spec =
|
285
|
-
spec.merge!(:optional => params)
|
284
|
+
spec = __method_specs[__key_for(method)] || { }
|
285
|
+
spec.merge!(:optional => Array(spec[:optional]) + params)
|
286
286
|
has_named_parameters method, spec, :strict, &block
|
287
287
|
end
|
288
288
|
end
|
@@ -322,14 +322,12 @@ module NamedParameters
|
|
322
322
|
|
323
323
|
# add instrumentation for class methods
|
324
324
|
def singleton_method_added name # :nodoc:
|
325
|
-
|
326
|
-
self.metaclass.send :alias_method,
|
327
|
-
#method = self.metaclass.instance_method name
|
325
|
+
__apply_method_spec :"self.#{name}" do
|
326
|
+
self.metaclass.send :alias_method, __aliased(name), name
|
328
327
|
owner = "#{self}::"
|
329
|
-
spec =
|
328
|
+
spec = __method_specs[__key_for(:"self.#{name}")]
|
330
329
|
metaclass.instance_eval do
|
331
|
-
|
332
|
-
intercept owner, name, spec
|
330
|
+
__intercept owner, name, spec
|
333
331
|
end
|
334
332
|
end
|
335
333
|
super
|
@@ -337,66 +335,44 @@ module NamedParameters
|
|
337
335
|
|
338
336
|
# add instrumentation for instance methods
|
339
337
|
def method_added name # :nodoc:
|
340
|
-
|
341
|
-
alias_method
|
342
|
-
#method = instance_method name
|
338
|
+
__apply_method_spec name do
|
339
|
+
alias_method __aliased(name), name
|
343
340
|
owner = "#{self}#"
|
344
|
-
spec =
|
345
|
-
|
346
|
-
intercept owner, name, spec
|
341
|
+
spec = __method_specs[__key_for(name)]
|
342
|
+
__intercept owner, name, spec
|
347
343
|
end
|
348
344
|
super
|
349
345
|
end
|
350
346
|
|
351
347
|
private
|
348
|
+
def __aliased name # :nodoc:
|
349
|
+
:"#{ALIAS_PREFIX}#{name}"
|
350
|
+
end
|
351
|
+
|
352
|
+
def __unaliased name # :nodoc:
|
353
|
+
name.gsub(/^#{ALIAS_PREFIX}/, '')
|
354
|
+
end
|
355
|
+
|
352
356
|
# apply instrumentation to method
|
353
|
-
def
|
354
|
-
if
|
355
|
-
@
|
357
|
+
def __apply_method_spec method # :nodoc:
|
358
|
+
if __method_specs.include? __key_for(method) and !__instrumenting?
|
359
|
+
@__instrumenting = true
|
356
360
|
yield method
|
357
|
-
@
|
361
|
+
@__instrumenting = false
|
358
362
|
end
|
359
363
|
end
|
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
364
|
|
389
365
|
# insert parameter validation prior to executing the instrumented method
|
390
|
-
def
|
366
|
+
def __intercept owner, name, spec # :nodoc:
|
391
367
|
class_eval(<<-CODE, __FILE__, __LINE__)
|
392
368
|
def #{name}(*args, &block)
|
393
369
|
# compute the fully-qualified name of the method
|
394
370
|
# this is used when reporting argument errors
|
395
371
|
fullname = "#{owner}#{name}"
|
396
|
-
|
372
|
+
|
397
373
|
# locate the argument representing the named parameters value
|
398
374
|
# for the method invocation
|
399
|
-
params
|
375
|
+
params = args.last
|
400
376
|
args << (params = { }) unless params.instance_of? Hash
|
401
377
|
|
402
378
|
# merge the declared default values for params into the arguments
|
@@ -405,7 +381,7 @@ module NamedParameters
|
|
405
381
|
defaults = { }
|
406
382
|
spec.each do |k, v|
|
407
383
|
next if k == :mode
|
408
|
-
v.each{ |entry| defaults.merge!(entry)
|
384
|
+
v.each{ |entry| entry.instance_of?(Hash) ? defaults.merge!(entry) : entry }
|
409
385
|
end
|
410
386
|
params = defaults.merge(params)
|
411
387
|
|
@@ -419,51 +395,27 @@ module NamedParameters
|
|
419
395
|
send method, *args, &block
|
420
396
|
end
|
421
397
|
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
|
446
398
|
end
|
447
399
|
|
448
400
|
# initialize specs table as needed
|
449
|
-
def
|
450
|
-
@
|
401
|
+
def __method_specs # :nodoc:
|
402
|
+
@__method_specs ||= { }
|
451
403
|
end
|
452
404
|
|
453
|
-
def
|
405
|
+
def __key_for method
|
454
406
|
type = method.to_s =~ /^self\./ ? :singleton : :instance
|
455
407
|
name = method.to_s.sub(/^self\./, '')
|
456
|
-
:"#{self}::#{type}.#{
|
408
|
+
:"#{self}::#{type}.#{__unaliased name}"
|
457
409
|
end
|
458
410
|
|
459
411
|
# check if in the process of instrumenting a method
|
460
|
-
def
|
461
|
-
@
|
412
|
+
def __instrumenting? # :nodoc:
|
413
|
+
@__instrumenting
|
462
414
|
end
|
463
415
|
|
464
|
-
# initialize the @
|
416
|
+
# initialize the @__instrumenting instance variable (housekeeping)
|
465
417
|
def self.extended base # :nodoc:
|
466
|
-
base.instance_variable_set(:@
|
418
|
+
base.instance_variable_set(:@__instrumenting, false)
|
467
419
|
end
|
468
420
|
end
|
469
421
|
end
|
data/named-parameters.gemspec
CHANGED
@@ -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.
|
8
|
+
s.version = "0.0.21"
|
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-
|
12
|
+
s.date = %q{2010-12-16}
|
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. }
|
@@ -32,7 +32,10 @@ Gem::Specification.new do |s|
|
|
32
32
|
"lib/named-parameters/module.rb",
|
33
33
|
"lib/named-parameters/object.rb",
|
34
34
|
"named-parameters.gemspec",
|
35
|
-
"spec/
|
35
|
+
"spec/has_named_parameters_spec.rb",
|
36
|
+
"spec/legacy_spec.rb",
|
37
|
+
"spec/recognizes_spec.rb",
|
38
|
+
"spec/requires_spec.rb",
|
36
39
|
"spec/spec_helper.rb"
|
37
40
|
]
|
38
41
|
s.homepage = %q{http://github.com/jurisgalang/named-parameters}
|
@@ -41,7 +44,10 @@ Gem::Specification.new do |s|
|
|
41
44
|
s.rubygems_version = %q{1.3.7}
|
42
45
|
s.summary = %q{Poor man's named-parameters in Ruby}
|
43
46
|
s.test_files = [
|
44
|
-
"spec/
|
47
|
+
"spec/has_named_parameters_spec.rb",
|
48
|
+
"spec/legacy_spec.rb",
|
49
|
+
"spec/recognizes_spec.rb",
|
50
|
+
"spec/requires_spec.rb",
|
45
51
|
"spec/spec_helper.rb"
|
46
52
|
]
|
47
53
|
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "NamedParameters::has_named_parameters" do
|
4
|
+
it "should allow declaration of the has_named_parameters clause" do
|
5
|
+
class Hnp0; end
|
6
|
+
Hnp0.should respond_to(:has_named_parameters)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "shouldn't complain if there are not declared parameters" do
|
10
|
+
class Hnp1
|
11
|
+
def initialize opts = { }; end
|
12
|
+
def method1 opts = { }; end
|
13
|
+
def self.method1 opts = { }; end
|
14
|
+
end
|
15
|
+
lambda { Hnp1.method1 }.should_not raise_error
|
16
|
+
lambda { @hasnamedparameters = Hnp1.new }.should_not raise_error
|
17
|
+
lambda { @hasnamedparameters.method1 }.should_not raise_error
|
18
|
+
end
|
19
|
+
end
|
@@ -3,7 +3,7 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
3
3
|
# TODO: reorganize the tests for clarity
|
4
4
|
# TODO: need separate spec for the NamedParameters.validate_specs method
|
5
5
|
# TODO: factor out specs for required, recognizes, and has_named_parameters
|
6
|
-
describe "NamedParameters" do
|
6
|
+
describe "NamedParameters::legacy" do
|
7
7
|
before :all do
|
8
8
|
class Foo
|
9
9
|
has_named_parameters :initialize, :required => :x, :optional => [ :y, :z ]
|
@@ -0,0 +1,160 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "NamedParameters::recognizes" do
|
4
|
+
it "should allow declaration of the recognizes clause" do
|
5
|
+
class Recognizes; end
|
6
|
+
Recognizes.should respond_to(:recognizes)
|
7
|
+
end
|
8
|
+
|
9
|
+
#it "complains if you don't declare a parameter when using the clause" do
|
10
|
+
# lambda {
|
11
|
+
# class Recognizes0
|
12
|
+
# recognizes
|
13
|
+
# def initialize opts = {}; end
|
14
|
+
# end
|
15
|
+
# }.should raise_error(ArgumentError)
|
16
|
+
#end
|
17
|
+
|
18
|
+
it "doesn't complain if you pass it an optional parameter" do
|
19
|
+
class Recognizes1
|
20
|
+
recognizes :bar
|
21
|
+
def initialize opts = {}; end
|
22
|
+
end
|
23
|
+
lambda { Recognizes1.new :bar => :bar }.should_not raise_error(ArgumentError)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "doesn't complain if you don't pass it an optional parameter" do
|
27
|
+
class Recognizes2
|
28
|
+
recognizes :bar
|
29
|
+
def initialize opts = {}; end
|
30
|
+
end
|
31
|
+
lambda { Recognizes2.new }.should_not raise_error(ArgumentError)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "complains if you pass it an undeclared parameter" do
|
35
|
+
class Recognizes3
|
36
|
+
recognizes :bar
|
37
|
+
def initialize opts = {}; end
|
38
|
+
end
|
39
|
+
lambda { Recognizes3.new :foo => :foo }.should raise_error(ArgumentError)
|
40
|
+
lambda { Recognizes3.new :foo => :foo, :bar => :bar }.should raise_error(ArgumentError)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "allows more than one optional parameters" do
|
44
|
+
class Recognizes4
|
45
|
+
recognizes :foo, :bar, :baz
|
46
|
+
def initialize opts = {}; end
|
47
|
+
end
|
48
|
+
lambda { Recognizes4.new :foo => :foo }.should_not raise_error(ArgumentError)
|
49
|
+
lambda { Recognizes4.new :bar => :bar }.should_not raise_error(ArgumentError)
|
50
|
+
lambda { Recognizes4.new :baz => :baz }.should_not raise_error(ArgumentError)
|
51
|
+
lambda { Recognizes4.new :foo => :foo, :bar => :bar }.should_not raise_error(ArgumentError)
|
52
|
+
lambda { Recognizes4.new :bar => :bar, :baz => :baz }.should_not raise_error(ArgumentError)
|
53
|
+
lambda { Recognizes4.new :foo => :foo, :bar => :bar, :baz => :baz }.should_not raise_error(ArgumentError)
|
54
|
+
end
|
55
|
+
|
56
|
+
it "allows you to specify default values for optional parameters" do
|
57
|
+
class Recognizes5
|
58
|
+
recognizes :foo => :foo
|
59
|
+
def initialize opts = {}
|
60
|
+
@foo = opts[:foo]
|
61
|
+
end
|
62
|
+
end
|
63
|
+
recognizes = Recognizes5.new
|
64
|
+
recognizes.instance_variable_get(:@foo).should eql(:foo)
|
65
|
+
end
|
66
|
+
|
67
|
+
it "allows you to override default values for optional parameters" do
|
68
|
+
class Recognizes6
|
69
|
+
recognizes :foo => :foo
|
70
|
+
def initialize opts = {}
|
71
|
+
@foo = opts[:foo]
|
72
|
+
end
|
73
|
+
end
|
74
|
+
recognizes = Recognizes6.new :foo => :bar
|
75
|
+
recognizes.instance_variable_get(:@foo).should eql(:bar)
|
76
|
+
end
|
77
|
+
|
78
|
+
it "allows you to declare parameters and default values as Array pairs" do
|
79
|
+
class Recognizes7
|
80
|
+
recognizes [ :foo, :foo ], [ :bar, :bar ]
|
81
|
+
def initialize opts = {}
|
82
|
+
@foo = opts[:foo]
|
83
|
+
@bar = opts[:bar]
|
84
|
+
end
|
85
|
+
end
|
86
|
+
recognizes = Recognizes7.new
|
87
|
+
recognizes.instance_variable_get(:@foo).should eql(:foo)
|
88
|
+
recognizes.instance_variable_get(:@bar).should eql(:bar)
|
89
|
+
end
|
90
|
+
|
91
|
+
it "allows you to declare parameters and default values as KV pairs" do
|
92
|
+
class Recognizes8
|
93
|
+
recognizes :foo => :foo, :bar => :bar
|
94
|
+
def initialize opts = {}
|
95
|
+
@foo = opts[:foo]
|
96
|
+
@bar = opts[:bar]
|
97
|
+
end
|
98
|
+
end
|
99
|
+
recognizes = Recognizes8.new
|
100
|
+
recognizes.instance_variable_get(:@foo).should eql(:foo)
|
101
|
+
recognizes.instance_variable_get(:@bar).should eql(:bar)
|
102
|
+
end
|
103
|
+
|
104
|
+
it "allows you to declare parameters and default values as Array and KV pairs" do
|
105
|
+
class Recognizes9
|
106
|
+
recognizes [ :foo, :foo ], { :bar => :bar }
|
107
|
+
def initialize opts = {}
|
108
|
+
@foo = opts[:foo]
|
109
|
+
@bar = opts[:bar]
|
110
|
+
end
|
111
|
+
end
|
112
|
+
recognizes = Recognizes9.new
|
113
|
+
recognizes.instance_variable_get(:@foo).should eql(:foo)
|
114
|
+
recognizes.instance_variable_get(:@bar).should eql(:bar)
|
115
|
+
end
|
116
|
+
|
117
|
+
it "allows you to declare parameters with and without default values" do
|
118
|
+
class Recognizes10
|
119
|
+
recognizes :foo, [ :bar, :bar ], :zoo, { :baz => :baz }, :quux
|
120
|
+
def initialize opts = {}
|
121
|
+
@foo = opts[:foo]
|
122
|
+
@bar = opts[:bar]
|
123
|
+
@baz = opts[:baz]
|
124
|
+
@zoo = opts[:zoo]
|
125
|
+
@quux = opts[:quux]
|
126
|
+
end
|
127
|
+
end
|
128
|
+
recognizes = Recognizes10.new(:foo => :foo, :zoo => :zoo, :quux => :quux)
|
129
|
+
recognizes.instance_variable_get(:@foo).should eql(:foo)
|
130
|
+
recognizes.instance_variable_get(:@bar).should eql(:bar)
|
131
|
+
recognizes.instance_variable_get(:@baz).should eql(:baz)
|
132
|
+
recognizes.instance_variable_get(:@zoo).should eql(:zoo)
|
133
|
+
recognizes.instance_variable_get(:@quux).should eql(:quux)
|
134
|
+
end
|
135
|
+
|
136
|
+
it "allows you to declare multiple requires clause and treat it as one" do
|
137
|
+
class Recognizes11
|
138
|
+
recognizes :foo
|
139
|
+
recognizes [ :bar, :bar ]
|
140
|
+
recognizes :baz => :baz
|
141
|
+
def initialize opts = { }
|
142
|
+
@foo = opts[:foo]
|
143
|
+
@bar = opts[:bar]
|
144
|
+
@baz = opts[:baz]
|
145
|
+
end
|
146
|
+
end
|
147
|
+
lambda { Recognizes11.new :foo => :foo, :bar => :bar, :baz => :baz }.should_not raise_error(ArgumentError)
|
148
|
+
lambda { Recognizes11.new :quux => :quux }.should raise_error(ArgumentError)
|
149
|
+
|
150
|
+
recognizes = Recognizes11.new
|
151
|
+
recognizes.instance_variable_get(:@foo).should be_nil
|
152
|
+
recognizes.instance_variable_get(:@bar).should eql(:bar)
|
153
|
+
recognizes.instance_variable_get(:@baz).should eql(:baz)
|
154
|
+
|
155
|
+
recognizes = Recognizes11.new :foo => :foo
|
156
|
+
recognizes.instance_variable_get(:@foo).should eql(:foo)
|
157
|
+
recognizes.instance_variable_get(:@bar).should eql(:bar)
|
158
|
+
recognizes.instance_variable_get(:@baz).should eql(:baz)
|
159
|
+
end
|
160
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "NamedParameters::requires" do
|
4
|
+
it "should allow declaration of the requires clause" do
|
5
|
+
class Requires; end
|
6
|
+
Requires.should respond_to(:requires)
|
7
|
+
end
|
8
|
+
|
9
|
+
#it "complains if you don't declare a parameter when using the clause" do
|
10
|
+
# lambda {
|
11
|
+
# class Requires0
|
12
|
+
# requires
|
13
|
+
# def initialize opts = {}; end
|
14
|
+
# end
|
15
|
+
# }.should raise_error(ArgumentError)
|
16
|
+
#end
|
17
|
+
|
18
|
+
it "complains if you don't pass it a required parameter" do
|
19
|
+
class Requires1
|
20
|
+
requires :bar
|
21
|
+
def initialize opts = {}; end
|
22
|
+
end
|
23
|
+
lambda { Requires1.new }.should raise_error(ArgumentError)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "doesn't complain if you pass it a required parameter" do
|
27
|
+
class Requires2
|
28
|
+
requires :bar
|
29
|
+
def initialize opts = {}; end
|
30
|
+
end
|
31
|
+
lambda { Requires2.new :bar => :bar }.should_not raise_error(ArgumentError)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "complains if you pass it an undeclared parameter" do
|
35
|
+
class Requires3
|
36
|
+
requires :bar
|
37
|
+
def initialize opts = {}; end
|
38
|
+
end
|
39
|
+
lambda { Requires3.new :foo => :foo }.should raise_error(ArgumentError)
|
40
|
+
lambda { Requires3.new :foo => :foo, :bar => :bar }.should raise_error(ArgumentError)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "allows more than one required parameters" do
|
44
|
+
class Requires4
|
45
|
+
requires :foo, :bar, :baz
|
46
|
+
def initialize opts = {}; end
|
47
|
+
end
|
48
|
+
lambda { Requires4.new :foo => :foo, :bar => :bar, :baz => :baz }.should_not raise_error(ArgumentError)
|
49
|
+
end
|
50
|
+
|
51
|
+
it "complains if not all of the required parameters was passed" do
|
52
|
+
class Requires5
|
53
|
+
requires :foo, :bar, :baz
|
54
|
+
def initialize opts = {}; end
|
55
|
+
end
|
56
|
+
lambda { Requires5.new :foo => :foo }.should raise_error(ArgumentError)
|
57
|
+
lambda { Requires5.new :foo => :foo, :bar => :bar }.should raise_error(ArgumentError)
|
58
|
+
lambda { Requires5.new :bar => :bar, :baz => :baz }.should raise_error(ArgumentError)
|
59
|
+
end
|
60
|
+
|
61
|
+
it "allows you to declare multiple requires clause and treat it as one" do
|
62
|
+
class Requires6
|
63
|
+
requires :foo
|
64
|
+
requires :bar
|
65
|
+
requires :baz
|
66
|
+
def initialize opts = {}; end
|
67
|
+
end
|
68
|
+
lambda { Requires6.new :foo => :foo, :bar => :bar, :baz => :baz }.should_not raise_error(ArgumentError)
|
69
|
+
lambda { Requires6.new :foo => :foo }.should raise_error(ArgumentError)
|
70
|
+
lambda { Requires6.new :foo => :foo, :bar => :bar }.should raise_error(ArgumentError)
|
71
|
+
lambda { Requires6.new :bar => :bar, :baz => :baz }.should raise_error(ArgumentError)
|
72
|
+
end
|
73
|
+
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 21
|
9
|
+
version: 0.0.21
|
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-
|
17
|
+
date: 2010-12-16 00:00:00 -08:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -111,7 +111,10 @@ files:
|
|
111
111
|
- lib/named-parameters/module.rb
|
112
112
|
- lib/named-parameters/object.rb
|
113
113
|
- named-parameters.gemspec
|
114
|
-
- spec/
|
114
|
+
- spec/has_named_parameters_spec.rb
|
115
|
+
- spec/legacy_spec.rb
|
116
|
+
- spec/recognizes_spec.rb
|
117
|
+
- spec/requires_spec.rb
|
115
118
|
- spec/spec_helper.rb
|
116
119
|
has_rdoc: true
|
117
120
|
homepage: http://github.com/jurisgalang/named-parameters
|
@@ -128,7 +131,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
128
131
|
requirements:
|
129
132
|
- - ">="
|
130
133
|
- !ruby/object:Gem::Version
|
131
|
-
hash:
|
134
|
+
hash: 1437560030022288609
|
132
135
|
segments:
|
133
136
|
- 0
|
134
137
|
version: "0"
|
@@ -148,5 +151,8 @@ signing_key:
|
|
148
151
|
specification_version: 3
|
149
152
|
summary: Poor man's named-parameters in Ruby
|
150
153
|
test_files:
|
151
|
-
- spec/
|
154
|
+
- spec/has_named_parameters_spec.rb
|
155
|
+
- spec/legacy_spec.rb
|
156
|
+
- spec/recognizes_spec.rb
|
157
|
+
- spec/requires_spec.rb
|
152
158
|
- spec/spec_helper.rb
|