option_initializer 1.1.2 → 1.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/option_initializer.rb +5 -5
- data/lib/option_initializer/version.rb +1 -1
- data/test/test_option_initializer.rb +32 -0
- metadata +1 -1
data/lib/option_initializer.rb
CHANGED
@@ -19,8 +19,8 @@ module OptionInitializer
|
|
19
19
|
|
20
20
|
const_set :VALIDATORS, []
|
21
21
|
|
22
|
-
def initialize base, options
|
23
|
-
validate options
|
22
|
+
def initialize base, options, need_validation
|
23
|
+
validate options if need_validation
|
24
24
|
@base = base
|
25
25
|
@options = options
|
26
26
|
end
|
@@ -50,7 +50,7 @@ module OptionInitializer
|
|
50
50
|
|
51
51
|
def merge opts
|
52
52
|
validate opts
|
53
|
-
self.class.new @base, @options.merge(opts)
|
53
|
+
self.class.new @base, @options.merge(opts), false
|
54
54
|
end
|
55
55
|
|
56
56
|
def validate hash
|
@@ -96,12 +96,12 @@ module OptionInitializer
|
|
96
96
|
singleton.send :undef_method, sym if singleton.method_defined?(sym)
|
97
97
|
singleton.send :define_method, sym do |*v, &b|
|
98
98
|
if b && v.empty?
|
99
|
-
oi.new self, sym => b
|
99
|
+
oi.new self, {sym => b}, true
|
100
100
|
elsif b && !v.empty?
|
101
101
|
raise ArgumentError,
|
102
102
|
"wrong number of arguments (#{v.length} for 0 when block given)"
|
103
103
|
elsif v.length == 1
|
104
|
-
oi.new self, sym => v.first
|
104
|
+
oi.new self, {sym => v.first}, true
|
105
105
|
else
|
106
106
|
raise ArgumentError,
|
107
107
|
"wrong number of arguments (#{v.length} for 1)"
|
@@ -25,14 +25,26 @@ class MyClass2
|
|
25
25
|
include OptionInitializer
|
26
26
|
include OptionInitializer
|
27
27
|
|
28
|
+
@@validate_count = 0
|
29
|
+
|
30
|
+
def self.reset_count
|
31
|
+
@@validate_count = 0
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.count
|
35
|
+
@@validate_count
|
36
|
+
end
|
37
|
+
|
28
38
|
option_initializer :aaa, :bbb, :ccc
|
29
39
|
option_validator do |k, v|
|
40
|
+
@@validate_count += 1
|
30
41
|
case k
|
31
42
|
when :aaa
|
32
43
|
raise ArgumentError if v == 0
|
33
44
|
end
|
34
45
|
end
|
35
46
|
option_validator do |k, v|
|
47
|
+
@@validate_count += 1
|
36
48
|
case k
|
37
49
|
when :aaa
|
38
50
|
raise ArgumentError if v < 0
|
@@ -107,6 +119,26 @@ class TestOptionInitializer < MiniTest::Unit::TestCase
|
|
107
119
|
assert_raises(ArgumentError) { MyClass2.aaa(1).aaa(1).new(:aaa => -2) }
|
108
120
|
assert_raises(ArgumentError) { MyClass2.aaa(1).aaa(1).new(:aaa => 0) }
|
109
121
|
assert_raises(ArgumentError) { MyClass2.new(:aaa => 0) }
|
122
|
+
|
123
|
+
MyClass2.reset_count
|
124
|
+
MyClass2.aaa(1)
|
125
|
+
assert_equal 2, MyClass2.count
|
126
|
+
|
127
|
+
MyClass2.reset_count
|
128
|
+
MyClass2.aaa(1).bbb(2)
|
129
|
+
assert_equal 2 + 2, MyClass2.count
|
130
|
+
|
131
|
+
MyClass2.reset_count
|
132
|
+
MyClass2.aaa(1).bbb(2).new(:aaa => 3)
|
133
|
+
assert_equal 2 + 2 + 2, MyClass2.count
|
134
|
+
|
135
|
+
MyClass2.reset_count
|
136
|
+
MyClass2.aaa(1).bbb(2).new
|
137
|
+
assert_equal 2 + 2, MyClass2.count
|
138
|
+
|
139
|
+
MyClass2.reset_count
|
140
|
+
MyClass2.new :aaa => 1, :bbb => 2
|
141
|
+
assert_equal 4, MyClass2.count
|
110
142
|
end
|
111
143
|
|
112
144
|
def test_readme
|