parameters 0.1.9 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -19,35 +19,42 @@ module Parameters
19
19
  #
20
20
  def initialize_params(values={})
21
21
  self.class.each_param do |param|
22
+ new_param = InstanceParam.new(
23
+ self,
24
+ param.name,
25
+ param.type,
26
+ param.description
27
+ )
28
+
22
29
  # do not override existing instance value if present
23
- if instance_variable_get("@#{param.name}".to_sym).nil?
24
- begin
25
- if param.value.kind_of?(Proc)
26
- value = if param.value.arity > 0
27
- param.value.call(self)
28
- else
29
- param.value.call()
30
- end
31
- else
30
+ if new_param.value.nil?
31
+ if param.value.kind_of?(Proc)
32
+ value = if param.value.arity > 0
33
+ param.value.call(self)
34
+ else
35
+ param.value.call()
36
+ end
37
+ else
38
+ begin
32
39
  value = param.value.clone
40
+ rescue TypeError
41
+ value = param.value
33
42
  end
34
- rescue TypeError
35
- value = param.value
36
43
  end
37
44
 
38
- instance_variable_set("@#{param.name}".to_sym,value)
45
+ new_param.value = value
39
46
  end
40
47
 
41
- self.params[param.name] = InstanceParam.new(self,param.name,param.description)
48
+ self.params[param.name] = new_param
42
49
  end
43
50
 
44
51
  self.params = values if values.kind_of?(Hash)
45
52
  end
46
53
 
47
54
  #
48
- # Initializes the parameters using initialize_params. If a +Hash+
55
+ # Initializes the parameters using initialize_params. If a `Hash`
49
56
  # is passed in as the first argument, it will be used to set the values
50
- # of parameters described within the Hash.
57
+ # of parameters described within the `Hash`.
51
58
  #
52
59
  def initialize(*args,&block)
53
60
  initialize_params(args.first)
@@ -62,12 +69,15 @@ module Parameters
62
69
  # @param [Hash] options
63
70
  # Additional options.
64
71
  #
65
- # @option options [String] :description
66
- # The description for the new parameter.
72
+ # @option options [Class, Array[Class]] :type
73
+ # The type to enforce the new parameter values to.
67
74
  #
68
75
  # @option options [Object, Proc] :default
69
76
  # The default value for the new parameter.
70
77
  #
78
+ # @option options [String] :description
79
+ # The description for the new parameter.
80
+ #
71
81
  # @return [InstanceParam]
72
82
  # The newly created instance parameter.
73
83
  #
@@ -80,7 +90,6 @@ module Parameters
80
90
  def parameter(name,options={})
81
91
  name = name.to_sym
82
92
  default = options[:default]
83
- description = options[:description]
84
93
 
85
94
  # resolve the default value
86
95
  if default.kind_of?(Proc)
@@ -93,25 +102,33 @@ module Parameters
93
102
  value = default
94
103
  end
95
104
 
105
+ # create the new parameter
106
+ new_param = InstanceParam.new(
107
+ self,
108
+ name,
109
+ options[:type],
110
+ options[:description]
111
+ )
112
+
96
113
  # set the instance variable
97
- instance_variable_set("@#{name}".to_sym,value)
114
+ new_param.value = value
98
115
 
99
116
  # add the new parameter
100
- self.params[name] = InstanceParam.new(self,name,description)
117
+ self.params[name] = new_param
101
118
 
102
119
  instance_eval %{
103
120
  # define the reader method for the parameter
104
121
  def #{name}
105
- instance_variable_get("@#{name}".to_sym)
122
+ get_param(#{name.inspect}).value
106
123
  end
107
124
 
108
125
  # define the writer method for the parameter
109
- def #{name}=(value)
110
- instance_variable_set("@#{name}".to_sym,value)
126
+ def #{name}=(new_value)
127
+ get_param(#{name.inspect}).value = new_value
111
128
  end
112
129
  }
113
130
 
114
- return params[name]
131
+ return new_param
115
132
  end
116
133
 
117
134
  #
@@ -243,10 +260,10 @@ module Parameters
243
260
 
244
261
  #
245
262
  # Requires that the instance parameters with specific names have
246
- # non +nil+ values.
263
+ # non `nil` values.
247
264
  #
248
265
  # @return [true]
249
- # All the instance parameters have non +nil+ values.
266
+ # All the instance parameters have non `nil` values.
250
267
  #
251
268
  # @raise [MissingParam]
252
269
  # One of the instance parameters was not set.
@@ -1,3 +1,3 @@
1
1
  module Parameters
2
- VERSION = '0.1.9'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -0,0 +1,84 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{parameters}
8
+ s.version = "0.2.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Postmodern"]
12
+ s.date = %q{2010-02-18}
13
+ s.description = %q{Parameters allows you to add annotated variables to your classes which may have configurable default values.}
14
+ s.email = %q{postmodern.mod3@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "ChangeLog.md",
17
+ "LICENSE.txt",
18
+ "README.md"
19
+ ]
20
+ s.files = [
21
+ ".gitignore",
22
+ ".specopts",
23
+ ".yardopts",
24
+ "ChangeLog.md",
25
+ "LICENSE.txt",
26
+ "README.md",
27
+ "Rakefile",
28
+ "lib/parameters.rb",
29
+ "lib/parameters/class_methods.rb",
30
+ "lib/parameters/class_param.rb",
31
+ "lib/parameters/exceptions.rb",
32
+ "lib/parameters/exceptions/missing_param.rb",
33
+ "lib/parameters/exceptions/param_not_found.rb",
34
+ "lib/parameters/extensions.rb",
35
+ "lib/parameters/extensions/meta.rb",
36
+ "lib/parameters/extensions/meta/object.rb",
37
+ "lib/parameters/instance_param.rb",
38
+ "lib/parameters/param.rb",
39
+ "lib/parameters/parameters.rb",
40
+ "lib/parameters/version.rb",
41
+ "parameters.gemspec",
42
+ "spec/class_param_spec.rb",
43
+ "spec/classes/custom_parameters.rb",
44
+ "spec/classes/inherited_parameters.rb",
45
+ "spec/classes/other_parameters.rb",
46
+ "spec/classes/test_parameters.rb",
47
+ "spec/instance_param_spec.rb",
48
+ "spec/parameters_spec.rb",
49
+ "spec/spec_helper.rb"
50
+ ]
51
+ s.has_rdoc = %q{yard}
52
+ s.homepage = %q{http://github.com/postmodern/parameters}
53
+ s.rdoc_options = ["--charset=UTF-8"]
54
+ s.require_paths = ["lib"]
55
+ s.rubygems_version = %q{1.3.5}
56
+ s.summary = %q{Allows you to add annotated variables to your classes}
57
+ s.test_files = [
58
+ "spec/parameters_spec.rb",
59
+ "spec/spec_helper.rb",
60
+ "spec/class_param_spec.rb",
61
+ "spec/instance_param_spec.rb",
62
+ "spec/classes/inherited_parameters.rb",
63
+ "spec/classes/test_parameters.rb",
64
+ "spec/classes/custom_parameters.rb",
65
+ "spec/classes/other_parameters.rb"
66
+ ]
67
+
68
+ if s.respond_to? :specification_version then
69
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
70
+ s.specification_version = 3
71
+
72
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
73
+ s.add_development_dependency(%q<rspec>, [">= 1.3.0"])
74
+ s.add_development_dependency(%q<yard>, [">= 0.5.3"])
75
+ else
76
+ s.add_dependency(%q<rspec>, [">= 1.3.0"])
77
+ s.add_dependency(%q<yard>, [">= 0.5.3"])
78
+ end
79
+ else
80
+ s.add_dependency(%q<rspec>, [">= 1.3.0"])
81
+ s.add_dependency(%q<yard>, [">= 0.5.3"])
82
+ end
83
+ end
84
+
@@ -0,0 +1,171 @@
1
+ require 'parameters/class_param'
2
+
3
+ require 'spec_helper'
4
+ require 'set'
5
+
6
+ describe Parameters::ClassParam do
7
+ describe "type coercion" do
8
+ it "should coerce values into Sets" do
9
+ param = Parameters::ClassParam.new(:x,Set)
10
+
11
+ param.value = [1, 2, 3, 2]
12
+ param.value.should == Set[1, 2, 3]
13
+ end
14
+
15
+ it "should coerce values into Sets with types" do
16
+ param = Parameters::ClassParam.new(:x,Set[Integer])
17
+
18
+ param.value = ['x', '0', '1', '2', '3']
19
+ param.value.should == Set[0, 1, 2, 3]
20
+ end
21
+
22
+ it "should coerce values into Arrays" do
23
+ param = Parameters::ClassParam.new(:x,Array)
24
+
25
+ param.value = Set[1, 2, 3]
26
+ param.value.should == [1, 2, 3]
27
+ end
28
+
29
+ it "should coerce values into Arrays with types" do
30
+ param = Parameters::ClassParam.new(:x,Array[Integer])
31
+
32
+ param.value = Set['1', '2', '3']
33
+ param.value.should == [1, 2, 3]
34
+ end
35
+
36
+ it "should coerce values into URIs" do
37
+ param = Parameters::ClassParam.new(:x,URI)
38
+
39
+ param.value = 'http://bla.com/'
40
+ param.value.should == URI::HTTP.build(:host => 'bla.com')
41
+ end
42
+
43
+ it "should coerce values into Regexp" do
44
+ param = Parameters::ClassParam.new(:x,Regexp)
45
+
46
+ param.value = 'x*'
47
+ param.value.should == /x*/
48
+ end
49
+
50
+ it "should coerce values into Dates" do
51
+ param = Parameters::ClassParam.new(:x,Date)
52
+
53
+ param.value = '2010-02-18'
54
+ param.value.should == Date.new(2010,2,18)
55
+ end
56
+
57
+ it "should coerce values into DateTimes" do
58
+ param = Parameters::ClassParam.new(:x,DateTime)
59
+
60
+ param.value = '2010-02-18T00:36:31-08:00'
61
+ param.value.should == DateTime.new(2010,2,18,0,36,31,Rational(-1,3),2299161)
62
+ end
63
+
64
+ it "should coerce values into Symbols" do
65
+ param = Parameters::ClassParam.new(:x,Symbol)
66
+
67
+ param.value = 'str'
68
+ param.value.should == :str
69
+ end
70
+
71
+ it "should coerce values into Strings" do
72
+ param = Parameters::ClassParam.new(:x,String)
73
+
74
+ param.value = :str
75
+ param.value.should == 'str'
76
+ end
77
+
78
+ it "should coerce values into Floats" do
79
+ param = Parameters::ClassParam.new(:x,Float)
80
+
81
+ param.value = '0.5'
82
+ param.value.should == 0.5
83
+ end
84
+
85
+ it "should coerce values into Integers" do
86
+ param = Parameters::ClassParam.new(:x,Integer)
87
+
88
+ param.value = '5'
89
+ param.value.should == 5
90
+ end
91
+
92
+ it "should coerce values into hex Integers" do
93
+ param = Parameters::ClassParam.new(:x,Integer)
94
+
95
+ param.value = '0xa'
96
+ param.value.should == 10
97
+ end
98
+
99
+ it "should coerce values into octal Integers" do
100
+ param = Parameters::ClassParam.new(:x,Integer)
101
+
102
+ param.value = '010'
103
+ param.value.should == 8
104
+ end
105
+
106
+ it "should coerce values into true boolean values" do
107
+ param = Parameters::ClassParam.new(:x,true)
108
+
109
+ param.value = true
110
+ param.value.should == true
111
+ end
112
+
113
+ it "should coerce non-false boolean values as true" do
114
+ param = Parameters::ClassParam.new(:x,true)
115
+
116
+ param.value = '1'
117
+ param.value.should == true
118
+ end
119
+
120
+ it "should coerce 'true' boolean values into true" do
121
+ param = Parameters::ClassParam.new(:x,true)
122
+
123
+ param.value = 'true'
124
+ param.value.should == true
125
+ end
126
+
127
+ it "should coerce :true boolean values into true" do
128
+ param = Parameters::ClassParam.new(:x,true)
129
+
130
+ param.value = :true
131
+ param.value.should == true
132
+ end
133
+
134
+ it "should coerce values into false" do
135
+ param = Parameters::ClassParam.new(:x,true)
136
+
137
+ param.value = false
138
+ param.value.should == false
139
+ end
140
+
141
+ it "should coerce 'false' boolean values into false" do
142
+ param = Parameters::ClassParam.new(:x,true)
143
+
144
+ param.value = 'false'
145
+ param.value.should == false
146
+ end
147
+
148
+ it "should coerce :false boolean values into false" do
149
+ param = Parameters::ClassParam.new(:x,true)
150
+
151
+ param.value = :false
152
+ param.value.should == false
153
+ end
154
+
155
+ it "should not coerce nil into a type" do
156
+ param = Parameters::ClassParam.new(:x,String)
157
+ obj1 = Object.new
158
+
159
+ param.value = nil
160
+ param.value.should be_nil
161
+ end
162
+
163
+ it "should not coerce unknown types" do
164
+ param = Parameters::ClassParam.new(:x)
165
+ obj1 = Object.new
166
+
167
+ param.value = obj1
168
+ param.value.should == obj1
169
+ end
170
+ end
171
+ end
@@ -0,0 +1,190 @@
1
+ require 'parameters/instance_param'
2
+
3
+ require 'spec_helper'
4
+ require 'set'
5
+
6
+ describe Parameters::InstanceParam do
7
+ it "should read values from another object" do
8
+ obj = Object.new
9
+ param = Parameters::InstanceParam.new(obj,:x)
10
+
11
+ obj.instance_variable_set(:"@x",5)
12
+ param.value.should == 5
13
+ end
14
+
15
+ it "should write values to another object" do
16
+ obj = Object.new
17
+ param = Parameters::InstanceParam.new(obj,:x)
18
+
19
+ param.value = 5
20
+ obj.instance_variable_get(:"@x").should == 5
21
+ end
22
+
23
+ describe "type coercion" do
24
+ before(:each) do
25
+ @obj = Object.new
26
+ end
27
+
28
+ it "should coerce values into Sets" do
29
+ param = Parameters::InstanceParam.new(@obj,:x,Set)
30
+
31
+ param.value = [1, 2, 3, 2]
32
+ param.value.should == Set[1, 2, 3]
33
+ end
34
+
35
+ it "should coerce values into Sets with types" do
36
+ param = Parameters::InstanceParam.new(@obj,:x,Set[Integer])
37
+
38
+ param.value = ['x', '0', '1', '2', '3']
39
+ param.value.should == Set[0, 1, 2, 3]
40
+ end
41
+
42
+ it "should coerce values into Arrays" do
43
+ param = Parameters::InstanceParam.new(@obj,:x,Array)
44
+
45
+ param.value = Set[1, 2, 3]
46
+ param.value.should == [1, 2, 3]
47
+ end
48
+
49
+ it "should coerce values into Arrays with types" do
50
+ param = Parameters::InstanceParam.new(@obj,:x,Array[Integer])
51
+
52
+ param.value = Set['1', '2', '3']
53
+ param.value.should == [1, 2, 3]
54
+ end
55
+
56
+ it "should coerce values into URIs" do
57
+ param = Parameters::InstanceParam.new(@obj,:x,URI)
58
+
59
+ param.value = 'http://bla.com/'
60
+ param.value.should == URI::HTTP.build(:host => 'bla.com')
61
+ end
62
+
63
+ it "should coerce values into Regexp" do
64
+ param = Parameters::InstanceParam.new(@obj,:x,Regexp)
65
+
66
+ param.value = 'x*'
67
+ param.value.should == /x*/
68
+ end
69
+
70
+ it "should coerce values into Dates" do
71
+ param = Parameters::InstanceParam.new(@obj,:x,Date)
72
+
73
+ param.value = '2010-02-18'
74
+ param.value.should == Date.new(2010,2,18)
75
+ end
76
+
77
+ it "should coerce values into DateTimes" do
78
+ param = Parameters::InstanceParam.new(@obj,:x,DateTime)
79
+
80
+ param.value = '2010-02-18T00:36:31-08:00'
81
+ param.value.should == DateTime.new(2010,2,18,0,36,31,Rational(-1,3),2299161)
82
+ end
83
+
84
+ it "should coerce values into Symbols" do
85
+ param = Parameters::InstanceParam.new(@obj,:x,Symbol)
86
+
87
+ param.value = 'str'
88
+ param.value.should == :str
89
+ end
90
+
91
+ it "should coerce values into Strings" do
92
+ param = Parameters::InstanceParam.new(@obj,:x,String)
93
+
94
+ param.value = :str
95
+ param.value.should == 'str'
96
+ end
97
+
98
+ it "should coerce values into Floats" do
99
+ param = Parameters::InstanceParam.new(@obj,:x,Float)
100
+
101
+ param.value = '0.5'
102
+ param.value.should == 0.5
103
+ end
104
+
105
+ it "should coerce values into Integers" do
106
+ param = Parameters::InstanceParam.new(@obj,:x,Integer)
107
+
108
+ param.value = '5'
109
+ param.value.should == 5
110
+ end
111
+
112
+ it "should coerce values into hex Integers" do
113
+ param = Parameters::InstanceParam.new(@obj,:x,Integer)
114
+
115
+ param.value = '0xa'
116
+ param.value.should == 10
117
+ end
118
+
119
+ it "should coerce values into octal Integers" do
120
+ param = Parameters::InstanceParam.new(@obj,:x,Integer)
121
+
122
+ param.value = '010'
123
+ param.value.should == 8
124
+ end
125
+
126
+ it "should coerce true boolean values" do
127
+ param = Parameters::InstanceParam.new(@obj,:x,true)
128
+
129
+ param.value = true
130
+ param.value.should == true
131
+ end
132
+
133
+ it "should coerce non-false boolean values into true" do
134
+ param = Parameters::InstanceParam.new(@obj,:x,true)
135
+
136
+ param.value = '1'
137
+ param.value.should == true
138
+ end
139
+
140
+ it "should coerce 'true' boolean values into true" do
141
+ param = Parameters::InstanceParam.new(@obj,:x,true)
142
+
143
+ param.value = 'true'
144
+ param.value.should == true
145
+ end
146
+
147
+ it "should coerce :true boolean values into true" do
148
+ param = Parameters::InstanceParam.new(@obj,:x,true)
149
+
150
+ param.value = :true
151
+ param.value.should == true
152
+ end
153
+
154
+ it "should coerce false values" do
155
+ param = Parameters::InstanceParam.new(@obj,:x,true)
156
+
157
+ param.value = false
158
+ param.value.should == false
159
+ end
160
+
161
+ it "should coerce 'false' boolean values into false" do
162
+ param = Parameters::InstanceParam.new(@obj,:x,true)
163
+
164
+ param.value = 'false'
165
+ param.value.should == false
166
+ end
167
+
168
+ it "should coerce :false boolean values into false" do
169
+ param = Parameters::InstanceParam.new(@obj,:x,true)
170
+
171
+ param.value = :false
172
+ param.value.should == false
173
+ end
174
+
175
+ it "should not coerce nil into a type" do
176
+ param = Parameters::InstanceParam.new(@obj,:x,String)
177
+
178
+ param.value = nil
179
+ param.value.should be_nil
180
+ end
181
+
182
+ it "should not coerce unknown types" do
183
+ param = Parameters::InstanceParam.new(@obj,:x)
184
+ obj1 = Object.new
185
+
186
+ param.value = obj1
187
+ param.value.should == obj1
188
+ end
189
+ end
190
+ end