parameters 0.2.3 → 0.3.0
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/.gemtest +0 -0
- data/.gitignore +10 -0
- data/ChangeLog.md +7 -0
- data/LICENSE.txt +1 -2
- data/README.md +15 -14
- data/Rakefile +6 -5
- data/gemspec.yml +5 -4
- data/lib/parameters/class_methods.rb +61 -22
- data/lib/parameters/class_param.rb +23 -0
- data/lib/parameters/instance_param.rb +21 -1
- data/lib/parameters/param.rb +17 -337
- data/lib/parameters/parameters.rb +54 -62
- data/lib/parameters/types.rb +17 -0
- data/lib/parameters/types/array.rb +83 -0
- data/lib/parameters/types/boolean.rb +47 -0
- data/lib/parameters/types/class.rb +45 -0
- data/lib/parameters/types/date.rb +28 -0
- data/lib/parameters/types/date_time.rb +29 -0
- data/lib/parameters/types/float.rb +26 -0
- data/lib/parameters/types/hash.rb +98 -0
- data/lib/parameters/types/integer.rb +31 -0
- data/lib/parameters/types/object.rb +50 -0
- data/lib/parameters/types/proc.rb +35 -0
- data/lib/parameters/types/regexp.rb +26 -0
- data/lib/parameters/types/set.rb +28 -0
- data/lib/parameters/types/string.rb +22 -0
- data/lib/parameters/types/symbol.rb +26 -0
- data/lib/parameters/types/time.rb +33 -0
- data/lib/parameters/types/type.rb +65 -0
- data/lib/parameters/types/types.rb +96 -0
- data/lib/parameters/types/uri.rb +41 -0
- data/lib/parameters/version.rb +2 -1
- data/parameters.gemspec +124 -7
- data/spec/class_param_spec.rb +1 -199
- data/spec/instance_param_spec.rb +3 -205
- data/spec/parameters_spec.rb +81 -58
- data/spec/spec_helper.rb +1 -1
- data/spec/types/array_spec.rb +39 -0
- data/spec/types/boolean_spec.rb +42 -0
- data/spec/types/class_spec.rb +31 -0
- data/spec/types/date_spec.rb +20 -0
- data/spec/types/date_time_spec.rb +20 -0
- data/spec/types/float_spec.rb +12 -0
- data/spec/types/hash_spec.rb +71 -0
- data/spec/types/integer_spec.rb +18 -0
- data/spec/types/object_spec.rb +24 -0
- data/spec/types/proc_spec.rb +21 -0
- data/spec/types/regexp_spec.rb +12 -0
- data/spec/types/set_spec.rb +40 -0
- data/spec/types/string_spec.rb +12 -0
- data/spec/types/symbol_spec.rb +12 -0
- data/spec/types/time_spec.rb +25 -0
- data/spec/types/types_spec.rb +82 -0
- data/spec/types/uri_spec.rb +23 -0
- metadata +107 -90
data/spec/parameters_spec.rb
CHANGED
@@ -24,41 +24,47 @@ describe Parameters do
|
|
24
24
|
end
|
25
25
|
|
26
26
|
context "in a Class" do
|
27
|
+
subject { TestParameters }
|
28
|
+
|
29
|
+
let(:inherited_class) { InheritedParameters }
|
30
|
+
|
27
31
|
it "should provide parameters" do
|
28
|
-
|
32
|
+
subject.params.should_not be_empty
|
29
33
|
end
|
30
34
|
|
31
35
|
it "should have default values for parameters" do
|
32
|
-
|
36
|
+
subject.param_value(:var_with_default).should == 'thing'
|
33
37
|
end
|
34
38
|
|
35
39
|
it "should provide class methods for paremters" do
|
36
|
-
|
37
|
-
|
40
|
+
subject.var = 1
|
41
|
+
subject.var.should == 1
|
38
42
|
end
|
39
43
|
|
40
44
|
it "should inherite the super-classes parameters" do
|
41
|
-
|
42
|
-
|
45
|
+
inherited_class.has_param?(:var).should == true
|
46
|
+
inherited_class.has_param?(:child_var).should == true
|
43
47
|
end
|
44
48
|
|
45
49
|
it "should provide direct access to the parameter objects" do
|
46
|
-
param =
|
50
|
+
param = subject.get_param(:var)
|
47
51
|
|
48
52
|
param.should_not be_nil
|
49
53
|
param.name.should == :var
|
50
54
|
end
|
51
55
|
|
52
56
|
it "should raise a ParamNotFound exception when directly accessing non-existent parameter objects" do
|
53
|
-
lambda {
|
57
|
+
lambda {
|
58
|
+
subject.get_param(:unknown)
|
59
|
+
}.should raise_error(Parameters::ParamNotFound)
|
54
60
|
end
|
55
61
|
|
56
62
|
it "should provide descriptions for parameters" do
|
57
|
-
|
63
|
+
subject.describe_param(:var).should_not be_empty
|
58
64
|
end
|
59
65
|
|
60
66
|
it "should be able to initialize parameters" do
|
61
|
-
obj =
|
67
|
+
obj = subject.new
|
62
68
|
|
63
69
|
obj.params.should_not be_empty
|
64
70
|
end
|
@@ -84,7 +90,7 @@ describe Parameters do
|
|
84
90
|
end
|
85
91
|
|
86
92
|
it "should be able to create an object with initial parameter values" do
|
87
|
-
obj =
|
93
|
+
obj = subject.new(:var => 2, :var_with_default => 'stuff')
|
88
94
|
|
89
95
|
obj.var.should == 2
|
90
96
|
obj.var_with_default.should == 'stuff'
|
@@ -92,130 +98,147 @@ describe Parameters do
|
|
92
98
|
end
|
93
99
|
|
94
100
|
context "in an Object" do
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
end
|
101
|
+
subject { TestParameters.new }
|
102
|
+
|
103
|
+
let(:inherited_params) { InheritedParameters.new }
|
99
104
|
|
100
105
|
it "should provide direct access to all parameters" do
|
101
|
-
|
102
|
-
|
106
|
+
subject.params[:var].should_not be_nil
|
107
|
+
subject.params[:var_with_default].should_not be_nil
|
103
108
|
end
|
104
109
|
|
105
110
|
it "should have default values for parameters" do
|
106
|
-
|
111
|
+
subject.param_value(:var_with_default).should == 'thing'
|
107
112
|
end
|
108
113
|
|
109
114
|
it "should provide instance methods for parameters" do
|
110
|
-
|
111
|
-
|
115
|
+
subject.var = 2
|
116
|
+
subject.var.should == 2
|
112
117
|
end
|
113
118
|
|
114
119
|
it "should set instance variables for paramters" do
|
115
|
-
|
120
|
+
subject.instance_variable_get(:@var_with_default).should == 'thing'
|
116
121
|
|
117
|
-
|
118
|
-
|
122
|
+
subject.var = 3
|
123
|
+
subject.instance_variable_get(:@var).should == 3
|
119
124
|
end
|
120
125
|
|
121
126
|
it "should allow using lambdas for the default values of parameters" do
|
122
|
-
|
127
|
+
other_params = TestParameters.new
|
123
128
|
|
124
|
-
|
129
|
+
subject.var_with_lambda.should_not == other_params.var_with_lambda
|
125
130
|
end
|
126
131
|
|
127
132
|
it "should contain the parameters from all ancestors" do
|
128
|
-
|
129
|
-
|
133
|
+
inherited_params.has_param?(:var).should == true
|
134
|
+
inherited_params.has_param?(:child_var).should == true
|
130
135
|
end
|
131
136
|
|
132
137
|
it "should provide direct access to the parameter objects" do
|
133
|
-
@param =
|
138
|
+
@param = subject.get_param(:var)
|
134
139
|
|
135
140
|
@param.should_not be_nil
|
136
141
|
@param.name.should == :var
|
137
142
|
end
|
138
143
|
|
139
144
|
it "should raise a ParamNotFound exception when directly accessing non-existent parameter objects" do
|
140
|
-
lambda {
|
145
|
+
lambda {
|
146
|
+
subject.get_param(:unknown)
|
147
|
+
}.should raise_error(Parameters::ParamNotFound)
|
148
|
+
end
|
149
|
+
|
150
|
+
it "should allow setting arbitrary parameters" do
|
151
|
+
subject.set_param(:var,2)
|
152
|
+
|
153
|
+
subject.get_param(:var).value.should == 2
|
154
|
+
end
|
155
|
+
|
156
|
+
it "should raise a ParamNotFound exception when setting non-existent parameters" do
|
157
|
+
lambda {
|
158
|
+
subject.set_param(:unknown,2)
|
159
|
+
}.should raise_error(Parameters::ParamNotFound)
|
141
160
|
end
|
142
161
|
|
143
162
|
it "should allow for setting parameters en-mass" do
|
144
|
-
|
163
|
+
subject.params = {:var => 3, :var_with_default => 7}
|
145
164
|
|
146
|
-
|
147
|
-
|
165
|
+
subject.param_value(:var).should == 3
|
166
|
+
subject.param_value(:var_with_default).should == 7
|
148
167
|
end
|
149
168
|
|
150
169
|
it "should allow for setting parameters en-mass from another object" do
|
151
170
|
obj = TestParameters.new(:var => 5, :var_with_default => 'hello')
|
152
|
-
|
171
|
+
subject.params = obj.params
|
153
172
|
|
154
|
-
|
155
|
-
|
173
|
+
subject.var.should == 5
|
174
|
+
subject.var_with_default.should == 'hello'
|
156
175
|
end
|
157
176
|
|
158
177
|
it "should allow for setting parameters en-mass from another class" do
|
159
|
-
|
178
|
+
subject.params = OtherParameters.params
|
160
179
|
|
161
|
-
|
162
|
-
|
180
|
+
subject.var.should be_nil
|
181
|
+
subject.var_with_default.should == 'other'
|
163
182
|
end
|
164
183
|
|
165
184
|
it "should provide descriptions for parameters" do
|
166
|
-
|
185
|
+
subject.describe_param(:var).should_not be_empty
|
167
186
|
end
|
168
187
|
|
169
188
|
it "should require that certain parameters have non nil values" do
|
170
189
|
lambda {
|
171
|
-
|
190
|
+
subject.instance_eval { require_params(:var_without_default) }
|
172
191
|
}.should raise_error(Parameters::MissingParam)
|
173
192
|
end
|
174
193
|
end
|
175
194
|
|
176
195
|
describe "runtime" do
|
196
|
+
subject { TestParameters.new }
|
197
|
+
|
177
198
|
before(:each) do
|
178
|
-
|
199
|
+
subject.parameter :new_param
|
200
|
+
|
201
|
+
subject.parameter :new_param_with_default,
|
202
|
+
:default => 5
|
179
203
|
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
:default => lambda { |obj| obj.new_param_with_default + 2 }
|
204
|
+
subject.parameter :new_param_with_lambda,
|
205
|
+
:default => lambda { |obj|
|
206
|
+
obj.new_param_with_default + 2
|
207
|
+
}
|
185
208
|
end
|
186
209
|
|
187
210
|
it "should allow for the addition of parameters" do
|
188
|
-
|
211
|
+
subject.has_param?(:new_param).should == true
|
189
212
|
end
|
190
213
|
|
191
214
|
it "should provide direct access to all parameters" do
|
192
|
-
|
193
|
-
|
194
|
-
|
215
|
+
subject.params[:new_param].should_not be_nil
|
216
|
+
subject.params[:new_param_with_default].should_not be_nil
|
217
|
+
subject.params[:new_param_with_lambda].should_not be_nil
|
195
218
|
end
|
196
219
|
|
197
220
|
it "should add reader methods for parameters" do
|
198
|
-
|
221
|
+
subject.new_param.should be_nil
|
199
222
|
end
|
200
223
|
|
201
224
|
it "should add writer methods for parameters" do
|
202
|
-
|
203
|
-
|
225
|
+
subject.new_param = 10
|
226
|
+
subject.new_param.should == 10
|
204
227
|
end
|
205
228
|
|
206
229
|
it "should set the instance variables of parameters" do
|
207
|
-
|
230
|
+
subject.instance_variable_get(:@new_param_with_default).should == 5
|
208
231
|
|
209
|
-
|
210
|
-
|
232
|
+
subject.new_param_with_default = 10
|
233
|
+
subject.instance_variable_get(:@new_param_with_default).should == 10
|
211
234
|
end
|
212
235
|
|
213
236
|
it "should initialize parameters with default values" do
|
214
|
-
|
237
|
+
subject.new_param_with_default.should == 5
|
215
238
|
end
|
216
239
|
|
217
240
|
it "should initialize pamareters with default lambda values" do
|
218
|
-
|
241
|
+
subject.new_param_with_lambda.should == 7
|
219
242
|
end
|
220
243
|
end
|
221
244
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'parameters/types/array'
|
3
|
+
require 'parameters/types/integer'
|
4
|
+
|
5
|
+
describe Parameters::Types::Array do
|
6
|
+
let(:array) { [1, 2, 3] }
|
7
|
+
|
8
|
+
subject { described_class }
|
9
|
+
|
10
|
+
describe "coerce" do
|
11
|
+
it "should call #to_a" do
|
12
|
+
subject.coerce(array.enum_for).should == array
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context "instance" do
|
17
|
+
let(:numbers) { %w[1 2 3] }
|
18
|
+
|
19
|
+
subject { described_class.new(Parameters::Types::Integer.new) }
|
20
|
+
|
21
|
+
it "should have a Ruby type" do
|
22
|
+
subject.to_ruby.should == Array[Integer]
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "#===" do
|
26
|
+
it "should check the type of each element" do
|
27
|
+
subject.should_not === numbers
|
28
|
+
|
29
|
+
subject.should === array
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "#coerce" do
|
34
|
+
it "should coerce each element" do
|
35
|
+
subject.coerce(numbers).should == array
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'parameters/types/boolean'
|
3
|
+
|
4
|
+
describe Parameters::Types::Boolean do
|
5
|
+
subject { described_class }
|
6
|
+
|
7
|
+
describe "#===" do
|
8
|
+
it "should match true" do
|
9
|
+
subject.should === true
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should match false" do
|
13
|
+
subject.should === false
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should not match anything else" do
|
17
|
+
subject.should_not === 1
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "#coerce" do
|
22
|
+
it "should map false to false" do
|
23
|
+
subject.coerce(false).should == false
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should map 'false' to false" do
|
27
|
+
subject.coerce('false').should == false
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should map :false to false" do
|
31
|
+
subject.coerce(:false).should == false
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should map nil to false" do
|
35
|
+
subject.coerce(nil).should == false
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should map everything else to true" do
|
39
|
+
subject.coerce(:foo).should == true
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'classes/custom_type'
|
3
|
+
require 'parameters/types/class'
|
4
|
+
|
5
|
+
describe Parameters::Types::Class do
|
6
|
+
let(:base_class) { CustomType }
|
7
|
+
let(:value) { :foo }
|
8
|
+
|
9
|
+
subject { described_class.new(base_class) }
|
10
|
+
|
11
|
+
it "should have a base-class" do
|
12
|
+
subject.base_class.should == base_class
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "#===" do
|
16
|
+
it "should check if the value inherits from the base-class" do
|
17
|
+
subject.should === base_class.new(value)
|
18
|
+
|
19
|
+
subject.should_not === Object.new
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "#coerce" do
|
24
|
+
it "should pass the value to the base-class" do
|
25
|
+
obj = subject.coerce(value)
|
26
|
+
|
27
|
+
obj.should be_kind_of(base_class)
|
28
|
+
obj.value.should == value
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'parameters/types/date'
|
3
|
+
|
4
|
+
describe Parameters::Types::Date do
|
5
|
+
subject { described_class }
|
6
|
+
|
7
|
+
describe "coerce" do
|
8
|
+
let(:string) { '2010-02-18' }
|
9
|
+
let(:date) { Date.new(2010,2,18) }
|
10
|
+
let(:time) { Time.parse(string) }
|
11
|
+
|
12
|
+
it "should call #to_date when possible" do
|
13
|
+
subject.coerce(time).should == date
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should parse Strings" do
|
17
|
+
subject.coerce(string).should == date
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'parameters/types/date_time'
|
3
|
+
|
4
|
+
describe Parameters::Types::DateTime do
|
5
|
+
subject { described_class }
|
6
|
+
|
7
|
+
describe "coerce" do
|
8
|
+
let(:string) { '2010-02-18T00:36:31-08:00' }
|
9
|
+
let(:time) { Time.parse(string) }
|
10
|
+
let(:date_time) { DateTime.parse(string) }
|
11
|
+
|
12
|
+
it "should call #to_datetime when possible" do
|
13
|
+
subject.coerce(time).should == date_time
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should parse Strings" do
|
17
|
+
subject.coerce(string).should == date_time
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'parameters/types/hash'
|
3
|
+
require 'parameters/types/symbol'
|
4
|
+
require 'parameters/types/integer'
|
5
|
+
|
6
|
+
describe Parameters::Types::Hash do
|
7
|
+
let(:array) { [:a, 1, :b, 2] }
|
8
|
+
let(:hash) { {:a => 1, :b => 2} }
|
9
|
+
|
10
|
+
describe "coerce" do
|
11
|
+
subject { described_class }
|
12
|
+
|
13
|
+
it "should accept Arrays" do
|
14
|
+
subject.coerce(array).should == hash
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should call #to_hash" do
|
18
|
+
subject.coerce(hash).should == hash
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context "instance" do
|
23
|
+
context "with key-type" do
|
24
|
+
let(:string_keys) { {'a' => 1, 'b' => 2} }
|
25
|
+
|
26
|
+
subject { described_class.new(Parameters::Types::Symbol,Parameters::Types::Object) }
|
27
|
+
|
28
|
+
it "should have a Ruby type" do
|
29
|
+
subject.to_ruby.should == Hash[Symbol => Object]
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "#===" do
|
33
|
+
it "should check the type of each key" do
|
34
|
+
subject.should_not === string_keys
|
35
|
+
|
36
|
+
subject.should === hash
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "#coerce" do
|
41
|
+
it "should coerce each key" do
|
42
|
+
subject.coerce(string_keys).should == hash
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context "with value-type" do
|
48
|
+
let(:string_values) { {:a => '1', :b => '2'} }
|
49
|
+
|
50
|
+
subject { described_class.new(Parameters::Types::Object,Parameters::Types::Integer) }
|
51
|
+
|
52
|
+
it "should have a Ruby type" do
|
53
|
+
subject.to_ruby.should == Hash[Object => Integer]
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "#===" do
|
57
|
+
it "should check the type of each value" do
|
58
|
+
subject.should_not === string_values
|
59
|
+
|
60
|
+
subject.should === hash
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
describe "#coerce" do
|
65
|
+
it "should coerce each value" do
|
66
|
+
subject.coerce(string_values).should == hash
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|