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.
Files changed (55) hide show
  1. data/.gemtest +0 -0
  2. data/.gitignore +10 -0
  3. data/ChangeLog.md +7 -0
  4. data/LICENSE.txt +1 -2
  5. data/README.md +15 -14
  6. data/Rakefile +6 -5
  7. data/gemspec.yml +5 -4
  8. data/lib/parameters/class_methods.rb +61 -22
  9. data/lib/parameters/class_param.rb +23 -0
  10. data/lib/parameters/instance_param.rb +21 -1
  11. data/lib/parameters/param.rb +17 -337
  12. data/lib/parameters/parameters.rb +54 -62
  13. data/lib/parameters/types.rb +17 -0
  14. data/lib/parameters/types/array.rb +83 -0
  15. data/lib/parameters/types/boolean.rb +47 -0
  16. data/lib/parameters/types/class.rb +45 -0
  17. data/lib/parameters/types/date.rb +28 -0
  18. data/lib/parameters/types/date_time.rb +29 -0
  19. data/lib/parameters/types/float.rb +26 -0
  20. data/lib/parameters/types/hash.rb +98 -0
  21. data/lib/parameters/types/integer.rb +31 -0
  22. data/lib/parameters/types/object.rb +50 -0
  23. data/lib/parameters/types/proc.rb +35 -0
  24. data/lib/parameters/types/regexp.rb +26 -0
  25. data/lib/parameters/types/set.rb +28 -0
  26. data/lib/parameters/types/string.rb +22 -0
  27. data/lib/parameters/types/symbol.rb +26 -0
  28. data/lib/parameters/types/time.rb +33 -0
  29. data/lib/parameters/types/type.rb +65 -0
  30. data/lib/parameters/types/types.rb +96 -0
  31. data/lib/parameters/types/uri.rb +41 -0
  32. data/lib/parameters/version.rb +2 -1
  33. data/parameters.gemspec +124 -7
  34. data/spec/class_param_spec.rb +1 -199
  35. data/spec/instance_param_spec.rb +3 -205
  36. data/spec/parameters_spec.rb +81 -58
  37. data/spec/spec_helper.rb +1 -1
  38. data/spec/types/array_spec.rb +39 -0
  39. data/spec/types/boolean_spec.rb +42 -0
  40. data/spec/types/class_spec.rb +31 -0
  41. data/spec/types/date_spec.rb +20 -0
  42. data/spec/types/date_time_spec.rb +20 -0
  43. data/spec/types/float_spec.rb +12 -0
  44. data/spec/types/hash_spec.rb +71 -0
  45. data/spec/types/integer_spec.rb +18 -0
  46. data/spec/types/object_spec.rb +24 -0
  47. data/spec/types/proc_spec.rb +21 -0
  48. data/spec/types/regexp_spec.rb +12 -0
  49. data/spec/types/set_spec.rb +40 -0
  50. data/spec/types/string_spec.rb +12 -0
  51. data/spec/types/symbol_spec.rb +12 -0
  52. data/spec/types/time_spec.rb +25 -0
  53. data/spec/types/types_spec.rb +82 -0
  54. data/spec/types/uri_spec.rb +23 -0
  55. metadata +107 -90
@@ -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
- TestParameters.params.should_not be_empty
32
+ subject.params.should_not be_empty
29
33
  end
30
34
 
31
35
  it "should have default values for parameters" do
32
- TestParameters.param_value(:var_with_default).should == 'thing'
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
- TestParameters.var = 1
37
- TestParameters.var.should == 1
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
- InheritedParameters.has_param?(:var).should == true
42
- InheritedParameters.has_param?(:child_var).should == true
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 = TestParameters.get_param(:var)
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 { TestParameters.get_param(:unknown) }.should raise_error(Parameters::ParamNotFound)
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
- TestParameters.describe_param(:var).should_not be_empty
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 = TestParameters.new
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 = TestParameters.new(:var => 2, :var_with_default => 'stuff')
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
- before(:each) do
96
- @test = TestParameters.new
97
- @test_inherited = InheritedParameters.new
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
- @test.params[:var].should_not be_nil
102
- @test.params[:var_with_default].should_not be_nil
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
- @test.param_value(:var_with_default).should == 'thing'
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
- @test.var = 2
111
- @test.var.should == 2
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
- @test.instance_variable_get(:@var_with_default).should == 'thing'
120
+ subject.instance_variable_get(:@var_with_default).should == 'thing'
116
121
 
117
- @test.var = 3
118
- @test.instance_variable_get(:@var).should == 3
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
- test2 = TestParameters.new
127
+ other_params = TestParameters.new
123
128
 
124
- @test.var_with_lambda.should_not == test2.var_with_lambda
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
- @test_inherited.has_param?(:var).should == true
129
- @test_inherited.has_param?(:child_var).should == true
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 = @test.get_param(:var)
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 { @test.get_param(:unknown) }.should raise_error(Parameters::ParamNotFound)
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
- @test.params = {:var => 3, :var_with_default => 7}
163
+ subject.params = {:var => 3, :var_with_default => 7}
145
164
 
146
- @test.param_value(:var).should == 3
147
- @test.param_value(:var_with_default).should == 7
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
- @test.params = obj.params
171
+ subject.params = obj.params
153
172
 
154
- @test.var.should == 5
155
- @test.var_with_default.should == 'hello'
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
- @test.params = OtherParameters.params
178
+ subject.params = OtherParameters.params
160
179
 
161
- @test.var.should be_nil
162
- @test.var_with_default.should == 'other'
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
- @test.describe_param(:var).should_not be_empty
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
- @test.instance_eval { require_params(:var_without_default) }
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
- @test = TestParameters.new
199
+ subject.parameter :new_param
200
+
201
+ subject.parameter :new_param_with_default,
202
+ :default => 5
179
203
 
180
- @test.parameter :new_param
181
- @test.parameter :new_param_with_default,
182
- :default => 5
183
- @test.parameter :new_param_with_lambda,
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
- @test.has_param?(:new_param).should == true
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
- @test.params[:new_param].should_not be_nil
193
- @test.params[:new_param_with_default].should_not be_nil
194
- @test.params[:new_param_with_lambda].should_not be_nil
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
- @test.new_param.should be_nil
221
+ subject.new_param.should be_nil
199
222
  end
200
223
 
201
224
  it "should add writer methods for parameters" do
202
- @test.new_param = 10
203
- @test.new_param.should == 10
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
- @test.instance_variable_get(:@new_param_with_default).should == 5
230
+ subject.instance_variable_get(:@new_param_with_default).should == 5
208
231
 
209
- @test.new_param_with_default = 10
210
- @test.instance_variable_get(:@new_param_with_default).should == 10
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
- @test.new_param_with_default.should == 5
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
- @test.new_param_with_lambda.should == 7
241
+ subject.new_param_with_lambda.should == 7
219
242
  end
220
243
  end
221
244
  end
@@ -1,4 +1,4 @@
1
- gem 'rspec', '~> 2.0.0'
1
+ gem 'rspec', '~> 2.4'
2
2
  require 'rspec'
3
3
 
4
4
  require 'parameters/version'
@@ -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,12 @@
1
+ require 'spec_helper'
2
+ require 'parameters/types/float'
3
+
4
+ describe Parameters::Types::Float do
5
+ subject { described_class }
6
+
7
+ describe "coerce" do
8
+ it "should call #to_f" do
9
+ subject.coerce('-0.001').should == -0.001
10
+ end
11
+ end
12
+ 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