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
@@ -1,3 +1,4 @@
1
1
  module Parameters
2
- VERSION = '0.2.3'
2
+ # The version of parameters
3
+ VERSION = '0.3.0'
3
4
  end
@@ -1,10 +1,127 @@
1
- # -*- encoding: utf-8 -*-
1
+ # encoding: utf-8
2
2
 
3
- begin
4
- Ore::Specification.new do |gemspec|
5
- # custom logic here
3
+ require 'yaml'
4
+
5
+ Gem::Specification.new do |gemspec|
6
+ files = if File.directory?('.git')
7
+ `git ls-files`.split($/)
8
+ elsif File.directory?('.hg')
9
+ `hg manifest`.split($/)
10
+ elsif File.directory?('.svn')
11
+ `svn ls -R`.split($/).select { |path| File.file?(path) }
12
+ else
13
+ Dir['{**/}{.*,*}'].select { |path| File.file?(path) }
14
+ end
15
+
16
+ filter_files = lambda { |paths|
17
+ case paths
18
+ when Array
19
+ (files & paths)
20
+ when String
21
+ (files & Dir[paths])
22
+ end
23
+ }
24
+
25
+ version = {
26
+ :file => 'lib/parameters/version.rb',
27
+ :constant => 'Parameters::VERSION'
28
+ }
29
+
30
+ defaults = {
31
+ 'name' => File.basename(File.dirname(__FILE__)),
32
+ 'files' => files,
33
+ 'executables' => filter_files['bin/*'].map { |path| File.basename(path) },
34
+ 'test_files' => filter_files['{test/{**/}*_test.rb,spec/{**/}*_spec.rb}'],
35
+ 'extra_doc_files' => filter_files['*.{txt,rdoc,md,markdown,tt,textile}'],
36
+ }
37
+
38
+ metadata = defaults.merge(YAML.load_file('gemspec.yml'))
39
+
40
+ gemspec.name = metadata.fetch('name',defaults[:name])
41
+ gemspec.version = if metadata['version']
42
+ metadata['version']
43
+ elsif File.file?(version[:file])
44
+ require File.join('.',version[:file])
45
+ eval(version[:constant])
46
+ end
47
+
48
+ gemspec.summary = metadata.fetch('summary',metadata['description'])
49
+ gemspec.description = metadata.fetch('description',metadata['summary'])
50
+
51
+ case metadata['license']
52
+ when Array
53
+ gemspec.licenses = metadata['license']
54
+ when String
55
+ gemspec.license = metadata['license']
56
+ end
57
+
58
+ case metadata['authors']
59
+ when Array
60
+ gemspec.authors = metadata['authors']
61
+ when String
62
+ gemspec.author = metadata['authors']
63
+ end
64
+
65
+ gemspec.email = metadata['email']
66
+ gemspec.homepage = metadata['homepage']
67
+
68
+ case metadata['require_paths']
69
+ when Array
70
+ gemspec.require_paths = metadata['require_paths']
71
+ when String
72
+ gemspec.require_path = metadata['require_paths']
73
+ end
74
+
75
+ gemspec.files = filter_files[metadata['files']]
76
+
77
+ gemspec.executables = metadata['executables']
78
+ gemspec.extensions = metadata['extensions']
79
+
80
+ if Gem::VERSION < '1.7.'
81
+ gemspec.default_executable = gemspec.executables.first
82
+ end
83
+
84
+ gemspec.test_files = filter_files[metadata['test_files']]
85
+
86
+ unless gemspec.files.include?('.document')
87
+ gemspec.extra_rdoc_files = metadata['extra_doc_files']
88
+ end
89
+
90
+ gemspec.post_install_message = metadata['post_install_message']
91
+ gemspec.requirements = metadata['requirements']
92
+
93
+ if gemspec.respond_to?(:required_ruby_version=)
94
+ gemspec.required_ruby_version = metadata['required_ruby_version']
95
+ end
96
+
97
+ if gemspec.respond_to?(:required_rubygems_version=)
98
+ gemspec.required_rubygems_version = metadata['required_rubygems_version']
99
+ end
100
+
101
+ parse_versions = lambda { |versions|
102
+ case versions
103
+ when Array
104
+ versions.map { |v| v.to_s }
105
+ when String
106
+ versions.split(/,\s*/)
107
+ end
108
+ }
109
+
110
+ if metadata['dependencies']
111
+ metadata['dependencies'].each do |name,versions|
112
+ gemspec.add_dependency(name,parse_versions[versions])
113
+ end
114
+ end
115
+
116
+ if metadata['runtime_dependencies']
117
+ metadata['runtime_dependencies'].each do |name,versions|
118
+ gemspec.add_runtime_dependency(name,parse_versions[versions])
119
+ end
120
+ end
121
+
122
+ if metadata['development_dependencies']
123
+ metadata['development_dependencies'].each do |name,versions|
124
+ gemspec.add_development_dependency(name,parse_versions[versions])
125
+ end
6
126
  end
7
- rescue NameError
8
- STDERR.puts "The 'parameters.gemspec' file requires Ore."
9
- STDERR.puts "Run `gem install ore` to install Ore."
10
127
  end
@@ -1,203 +1,5 @@
1
- require 'parameters/class_param'
2
-
3
1
  require 'spec_helper'
4
- require 'classes/custom_type'
5
- require 'set'
2
+ require 'parameters/class_param'
6
3
 
7
4
  describe Parameters::ClassParam do
8
- describe "type coercion" do
9
- it "should coerce values into Hashes" do
10
- param = Parameters::ClassParam.new(:x,Hash)
11
-
12
- param.value = 2
13
- param.value.should == {2 => true}
14
- end
15
-
16
- it "should coerce keys and values within Hashes" do
17
- param = Parameters::ClassParam.new(:x,Hash[Symbol => Object])
18
-
19
- param.value = {'a' => true}
20
- param.value.should == {:a => true}
21
- end
22
-
23
- it "should coerce values into Sets" do
24
- param = Parameters::ClassParam.new(:x,Set)
25
-
26
- param.value = [1, 2, 3, 2]
27
- param.value.should == Set[1, 2, 3]
28
- end
29
-
30
- it "should coerce values into Sets with types" do
31
- param = Parameters::ClassParam.new(:x,Set[Integer])
32
-
33
- param.value = ['x', '0', '1', '2', '3']
34
- param.value.should == Set[0, 1, 2, 3]
35
- end
36
-
37
- it "should coerce values into Arrays" do
38
- param = Parameters::ClassParam.new(:x,Array)
39
-
40
- param.value = Set[1, 2, 3]
41
- param.value.should == [1, 2, 3]
42
- end
43
-
44
- it "should coerce values into Arrays with types" do
45
- param = Parameters::ClassParam.new(:x,Array[Integer])
46
-
47
- param.value = Set['1', '2', '3']
48
- param.value.should =~ [1, 2, 3]
49
- end
50
-
51
- it "should coerce values into URIs" do
52
- param = Parameters::ClassParam.new(:x,URI)
53
-
54
- param.value = 'http://bla.com/'
55
- param.value.should == URI::HTTP.build(:host => 'bla.com')
56
- end
57
-
58
- it "should coerce values into Regexp" do
59
- param = Parameters::ClassParam.new(:x,Regexp)
60
-
61
- param.value = 'x*'
62
- param.value.should == /x*/
63
- end
64
-
65
- it "should coerce values into Dates" do
66
- param = Parameters::ClassParam.new(:x,Date)
67
-
68
- param.value = '2010-02-18'
69
- param.value.should == Date.new(2010,2,18)
70
- end
71
-
72
- it "should coerce values into DateTimes" do
73
- param = Parameters::ClassParam.new(:x,DateTime)
74
-
75
- param.value = '2010-02-18T00:36:31-08:00'
76
- param.value.should == DateTime.new(2010,2,18,0,36,31,Rational(-1,3),2299161)
77
- end
78
-
79
- it "should coerce values into Symbols" do
80
- param = Parameters::ClassParam.new(:x,Symbol)
81
-
82
- param.value = 'str'
83
- param.value.should == :str
84
- end
85
-
86
- it "should coerce values into Strings" do
87
- param = Parameters::ClassParam.new(:x,String)
88
-
89
- param.value = :str
90
- param.value.should == 'str'
91
- end
92
-
93
- it "should coerce values into Floats" do
94
- param = Parameters::ClassParam.new(:x,Float)
95
-
96
- param.value = '0.5'
97
- param.value.should == 0.5
98
- end
99
-
100
- it "should coerce values into Integers" do
101
- param = Parameters::ClassParam.new(:x,Integer)
102
-
103
- param.value = '5'
104
- param.value.should == 5
105
- end
106
-
107
- it "should coerce values into hex Integers" do
108
- param = Parameters::ClassParam.new(:x,Integer)
109
-
110
- param.value = '0xa'
111
- param.value.should == 10
112
- end
113
-
114
- it "should coerce values into octal Integers" do
115
- param = Parameters::ClassParam.new(:x,Integer)
116
-
117
- param.value = '010'
118
- param.value.should == 8
119
- end
120
-
121
- it "should coerce values into true boolean values" do
122
- param = Parameters::ClassParam.new(:x,true)
123
-
124
- param.value = true
125
- param.value.should == true
126
- end
127
-
128
- it "should coerce non-false boolean values as true" do
129
- param = Parameters::ClassParam.new(:x,true)
130
-
131
- param.value = '1'
132
- param.value.should == true
133
- end
134
-
135
- it "should coerce 'true' boolean values into true" do
136
- param = Parameters::ClassParam.new(:x,true)
137
-
138
- param.value = 'true'
139
- param.value.should == true
140
- end
141
-
142
- it "should coerce :true boolean values into true" do
143
- param = Parameters::ClassParam.new(:x,true)
144
-
145
- param.value = :true
146
- param.value.should == true
147
- end
148
-
149
- it "should coerce values into false" do
150
- param = Parameters::ClassParam.new(:x,true)
151
-
152
- param.value = false
153
- param.value.should == false
154
- end
155
-
156
- it "should coerce 'false' boolean values into false" do
157
- param = Parameters::ClassParam.new(:x,true)
158
-
159
- param.value = 'false'
160
- param.value.should == false
161
- end
162
-
163
- it "should coerce :false boolean values into false" do
164
- param = Parameters::ClassParam.new(:x,true)
165
-
166
- param.value = :false
167
- param.value.should == false
168
- end
169
-
170
- it "should coerce values using a Proc for the type" do
171
- coercion_logic = lambda { |x| x.to_i.floor }
172
- param = Parameters::ClassParam.new(:x,coercion_logic)
173
-
174
- param.value = 2.5
175
- param.value.should == 2
176
- end
177
-
178
- it "should coerce values using custom Classes" do
179
- original_value = Object.new
180
- param = Parameters::ClassParam.new(:x,CustomType)
181
-
182
- param.value = original_value
183
- param.value.class.should == CustomType
184
- param.value.value.should == original_value
185
- end
186
-
187
- it "should not coerce nil into a type" do
188
- param = Parameters::ClassParam.new(:x,String)
189
- obj1 = Object.new
190
-
191
- param.value = nil
192
- param.value.should be_nil
193
- end
194
-
195
- it "should not coerce unknown types" do
196
- param = Parameters::ClassParam.new(:x)
197
- obj1 = Object.new
198
-
199
- param.value = obj1
200
- param.value.should == obj1
201
- end
202
- end
203
5
  end
@@ -1,13 +1,10 @@
1
- require 'parameters/instance_param'
2
-
3
1
  require 'spec_helper'
4
- require 'classes/custom_type'
5
- require 'set'
2
+ require 'parameters/instance_param'
6
3
 
7
4
  describe Parameters::InstanceParam do
8
5
  it "should read values from another object" do
9
6
  obj = Object.new
10
- param = Parameters::InstanceParam.new(obj,:x)
7
+ param = described_class.new(obj,:x)
11
8
 
12
9
  obj.instance_variable_set(:"@x",5)
13
10
  param.value.should == 5
@@ -15,208 +12,9 @@ describe Parameters::InstanceParam do
15
12
 
16
13
  it "should write values to another object" do
17
14
  obj = Object.new
18
- param = Parameters::InstanceParam.new(obj,:x)
15
+ param = described_class.new(obj,:x)
19
16
 
20
17
  param.value = 5
21
18
  obj.instance_variable_get(:"@x").should == 5
22
19
  end
23
-
24
- describe "type coercion" do
25
- before(:each) do
26
- @obj = Object.new
27
- end
28
-
29
- it "should coerce values into Hashes" do
30
- param = Parameters::InstanceParam.new(@obj,:x,Hash)
31
-
32
- param.value = 2
33
- param.value.should == {2 => true}
34
- end
35
-
36
- it "should coerce keys and values within Hashes" do
37
- param = Parameters::InstanceParam.new(@obj,:x,Hash[Symbol => Object])
38
-
39
- param.value = {'a' => true}
40
- param.value.should == {:a => true}
41
- end
42
-
43
- it "should coerce values into Sets" do
44
- param = Parameters::InstanceParam.new(@obj,:x,Set)
45
-
46
- param.value = [1, 2, 3, 2]
47
- param.value.should == Set[1, 2, 3]
48
- end
49
-
50
- it "should coerce values into Sets with types" do
51
- param = Parameters::InstanceParam.new(@obj,:x,Set[Integer])
52
-
53
- param.value = ['x', '0', '1', '2', '3']
54
- param.value.should == Set[0, 1, 2, 3]
55
- end
56
-
57
- it "should coerce values into Arrays" do
58
- param = Parameters::InstanceParam.new(@obj,:x,Array)
59
-
60
- param.value = Set[1, 2, 3]
61
- param.value.should == [1, 2, 3]
62
- end
63
-
64
- it "should coerce values into Arrays with types" do
65
- param = Parameters::InstanceParam.new(@obj,:x,Array[Integer])
66
-
67
- param.value = Set['1', '2', '3']
68
- param.value.should =~ [1, 2, 3]
69
- end
70
-
71
- it "should coerce values into URIs" do
72
- param = Parameters::InstanceParam.new(@obj,:x,URI)
73
-
74
- param.value = 'http://bla.com/'
75
- param.value.should == URI::HTTP.build(:host => 'bla.com')
76
- end
77
-
78
- it "should coerce values into Regexp" do
79
- param = Parameters::InstanceParam.new(@obj,:x,Regexp)
80
-
81
- param.value = 'x*'
82
- param.value.should == /x*/
83
- end
84
-
85
- it "should coerce values into Dates" do
86
- param = Parameters::InstanceParam.new(@obj,:x,Date)
87
-
88
- param.value = '2010-02-18'
89
- param.value.should == Date.new(2010,2,18)
90
- end
91
-
92
- it "should coerce values into DateTimes" do
93
- param = Parameters::InstanceParam.new(@obj,:x,DateTime)
94
-
95
- param.value = '2010-02-18T00:36:31-08:00'
96
- param.value.should == DateTime.new(2010,2,18,0,36,31,Rational(-1,3),2299161)
97
- end
98
-
99
- it "should coerce values into Symbols" do
100
- param = Parameters::InstanceParam.new(@obj,:x,Symbol)
101
-
102
- param.value = 'str'
103
- param.value.should == :str
104
- end
105
-
106
- it "should coerce values into Strings" do
107
- param = Parameters::InstanceParam.new(@obj,:x,String)
108
-
109
- param.value = :str
110
- param.value.should == 'str'
111
- end
112
-
113
- it "should coerce values into Floats" do
114
- param = Parameters::InstanceParam.new(@obj,:x,Float)
115
-
116
- param.value = '0.5'
117
- param.value.should == 0.5
118
- end
119
-
120
- it "should coerce values into Integers" do
121
- param = Parameters::InstanceParam.new(@obj,:x,Integer)
122
-
123
- param.value = '5'
124
- param.value.should == 5
125
- end
126
-
127
- it "should coerce values into hex Integers" do
128
- param = Parameters::InstanceParam.new(@obj,:x,Integer)
129
-
130
- param.value = '0xa'
131
- param.value.should == 10
132
- end
133
-
134
- it "should coerce values into octal Integers" do
135
- param = Parameters::InstanceParam.new(@obj,:x,Integer)
136
-
137
- param.value = '010'
138
- param.value.should == 8
139
- end
140
-
141
- it "should coerce true boolean values" do
142
- param = Parameters::InstanceParam.new(@obj,:x,true)
143
-
144
- param.value = true
145
- param.value.should == true
146
- end
147
-
148
- it "should coerce non-false boolean values into true" do
149
- param = Parameters::InstanceParam.new(@obj,:x,true)
150
-
151
- param.value = '1'
152
- param.value.should == true
153
- end
154
-
155
- it "should coerce 'true' boolean values into true" do
156
- param = Parameters::InstanceParam.new(@obj,:x,true)
157
-
158
- param.value = 'true'
159
- param.value.should == true
160
- end
161
-
162
- it "should coerce :true boolean values into true" do
163
- param = Parameters::InstanceParam.new(@obj,:x,true)
164
-
165
- param.value = :true
166
- param.value.should == true
167
- end
168
-
169
- it "should coerce false values" do
170
- param = Parameters::InstanceParam.new(@obj,:x,true)
171
-
172
- param.value = false
173
- param.value.should == false
174
- end
175
-
176
- it "should coerce 'false' boolean values into false" do
177
- param = Parameters::InstanceParam.new(@obj,:x,true)
178
-
179
- param.value = 'false'
180
- param.value.should == false
181
- end
182
-
183
- it "should coerce :false boolean values into false" do
184
- param = Parameters::InstanceParam.new(@obj,:x,true)
185
-
186
- param.value = :false
187
- param.value.should == false
188
- end
189
-
190
- it "should coerce values using a Proc for the type" do
191
- coercion_logic = lambda { |x| x.to_i.floor }
192
- param = Parameters::InstanceParam.new(@obj,:x,coercion_logic)
193
-
194
- param.value = 2.5
195
- param.value.should == 2
196
- end
197
-
198
- it "should coerce values using custom Classes" do
199
- original_value = Object.new
200
- param = Parameters::InstanceParam.new(@obj,:x,CustomType)
201
-
202
- param.value = original_value
203
- param.value.class.should == CustomType
204
- param.value.value.should == original_value
205
- end
206
-
207
- it "should not coerce nil into a type" do
208
- param = Parameters::InstanceParam.new(@obj,:x,String)
209
-
210
- param.value = nil
211
- param.value.should be_nil
212
- end
213
-
214
- it "should not coerce unknown types" do
215
- param = Parameters::InstanceParam.new(@obj,:x)
216
- obj1 = Object.new
217
-
218
- param.value = obj1
219
- param.value.should == obj1
220
- end
221
- end
222
20
  end