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
@@ -19,33 +19,7 @@ module Parameters
|
|
19
19
|
#
|
20
20
|
def initialize_params(values={})
|
21
21
|
self.class.each_param do |param|
|
22
|
-
|
23
|
-
self,
|
24
|
-
param.name,
|
25
|
-
param.type,
|
26
|
-
param.description
|
27
|
-
)
|
28
|
-
|
29
|
-
# do not override existing instance value if present
|
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
|
39
|
-
value = param.value.clone
|
40
|
-
rescue TypeError
|
41
|
-
value = param.value
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
new_param.value = value
|
46
|
-
end
|
47
|
-
|
48
|
-
self.params[param.name] = new_param
|
22
|
+
self.params[param.name] = param.to_instance(self)
|
49
23
|
end
|
50
24
|
|
51
25
|
self.params = values if values.kind_of?(Hash)
|
@@ -88,33 +62,7 @@ module Parameters
|
|
88
62
|
# obj.parameter('var',:default => 3, :description => 'my variable')
|
89
63
|
#
|
90
64
|
def parameter(name,options={})
|
91
|
-
name
|
92
|
-
default = options[:default]
|
93
|
-
|
94
|
-
# resolve the default value
|
95
|
-
if default.kind_of?(Proc)
|
96
|
-
value = if default.arity > 0
|
97
|
-
default.call(self)
|
98
|
-
else
|
99
|
-
default.call()
|
100
|
-
end
|
101
|
-
else
|
102
|
-
value = default
|
103
|
-
end
|
104
|
-
|
105
|
-
# create the new parameter
|
106
|
-
new_param = InstanceParam.new(
|
107
|
-
self,
|
108
|
-
name,
|
109
|
-
options[:type],
|
110
|
-
options[:description]
|
111
|
-
)
|
112
|
-
|
113
|
-
# set the instance variable
|
114
|
-
new_param.value = value
|
115
|
-
|
116
|
-
# add the new parameter
|
117
|
-
self.params[name] = new_param
|
65
|
+
name = name.to_sym
|
118
66
|
|
119
67
|
instance_eval %{
|
120
68
|
# define the reader method for the parameter
|
@@ -128,6 +76,17 @@ module Parameters
|
|
128
76
|
end
|
129
77
|
}
|
130
78
|
|
79
|
+
# create the new parameter
|
80
|
+
new_param = InstanceParam.new(
|
81
|
+
self,
|
82
|
+
name,
|
83
|
+
options[:type],
|
84
|
+
options[:description],
|
85
|
+
options[:default]
|
86
|
+
)
|
87
|
+
|
88
|
+
# add the new parameter
|
89
|
+
self.params[name] = new_param
|
131
90
|
return new_param
|
132
91
|
end
|
133
92
|
|
@@ -162,11 +121,13 @@ module Parameters
|
|
162
121
|
name = name.to_sym
|
163
122
|
|
164
123
|
if has_param?(name)
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
124
|
+
self.params[name].value = case value
|
125
|
+
when Parameters::ClassParam,
|
126
|
+
Parameters::InstanceParam
|
127
|
+
value.value
|
128
|
+
else
|
129
|
+
value
|
130
|
+
end
|
170
131
|
end
|
171
132
|
end
|
172
133
|
end
|
@@ -212,12 +173,43 @@ module Parameters
|
|
212
173
|
name = name.to_sym
|
213
174
|
|
214
175
|
unless has_param?(name)
|
215
|
-
raise(Parameters::ParamNotFound,"parameter #{name.to_s.dump} was not found within #{self.
|
176
|
+
raise(Parameters::ParamNotFound,"parameter #{name.to_s.dump} was not found within #{self.inspect}")
|
216
177
|
end
|
217
178
|
|
218
179
|
return self.params[name]
|
219
180
|
end
|
220
181
|
|
182
|
+
#
|
183
|
+
# Sets an instance parameter.
|
184
|
+
#
|
185
|
+
# @param [Symbol, String] name
|
186
|
+
# The name of the instance parameter.
|
187
|
+
#
|
188
|
+
# @param [Object] value
|
189
|
+
# The new value for the instance parameter.
|
190
|
+
#
|
191
|
+
# @return [Object]
|
192
|
+
# The new value of the instance parameter.
|
193
|
+
#
|
194
|
+
# @raise [ParamNotfound]
|
195
|
+
# No instance parameter with the specified name could be found.
|
196
|
+
#
|
197
|
+
# @example
|
198
|
+
# obj.set_param('var',2)
|
199
|
+
# # => 2
|
200
|
+
#
|
201
|
+
# @since 0.3.0
|
202
|
+
#
|
203
|
+
def set_param(name,value)
|
204
|
+
name = name.to_sym
|
205
|
+
|
206
|
+
unless has_param?(name)
|
207
|
+
raise(Parameters::ParamNotFound,"parameter #{name.to_s.dump} was not found within #{self.to_s.dump}")
|
208
|
+
end
|
209
|
+
|
210
|
+
return self.params[name].value = value
|
211
|
+
end
|
212
|
+
|
221
213
|
#
|
222
214
|
# Returns the description of the parameter with a specific name.
|
223
215
|
#
|
@@ -272,8 +264,8 @@ module Parameters
|
|
272
264
|
names.each do |name|
|
273
265
|
name = name.to_s
|
274
266
|
|
275
|
-
if instance_variable_get("@#{name}"
|
276
|
-
raise(Parameters::MissingParam,"parameter #{name.dump} has no value"
|
267
|
+
if instance_variable_get(:"@#{name}").nil?
|
268
|
+
raise(Parameters::MissingParam,"parameter #{name.dump} has no value")
|
277
269
|
end
|
278
270
|
end
|
279
271
|
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'parameters/types/type'
|
2
|
+
require 'parameters/types/object'
|
3
|
+
require 'parameters/types/class'
|
4
|
+
require 'parameters/types/boolean'
|
5
|
+
require 'parameters/types/integer'
|
6
|
+
require 'parameters/types/float'
|
7
|
+
require 'parameters/types/symbol'
|
8
|
+
require 'parameters/types/string'
|
9
|
+
require 'parameters/types/date'
|
10
|
+
require 'parameters/types/time'
|
11
|
+
require 'parameters/types/date_time'
|
12
|
+
require 'parameters/types/regexp'
|
13
|
+
require 'parameters/types/uri'
|
14
|
+
require 'parameters/types/array'
|
15
|
+
require 'parameters/types/set'
|
16
|
+
require 'parameters/types/hash'
|
17
|
+
require 'parameters/types/types'
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'parameters/types/object'
|
2
|
+
|
3
|
+
module Parameters
|
4
|
+
module Types
|
5
|
+
class Array < Object
|
6
|
+
|
7
|
+
# The type to coerce all Array elements with
|
8
|
+
attr_reader :element_type
|
9
|
+
|
10
|
+
#
|
11
|
+
# Initializes the Array type.
|
12
|
+
#
|
13
|
+
# @param [Type, nil] element_type
|
14
|
+
# Optional type for the elements of the Array.
|
15
|
+
#
|
16
|
+
def initialize(element_type)
|
17
|
+
@element_type = element_type
|
18
|
+
end
|
19
|
+
|
20
|
+
#
|
21
|
+
# Coerces a value into an Array.
|
22
|
+
#
|
23
|
+
# @param [#to_a, ::Object] value
|
24
|
+
# The value to coerce.
|
25
|
+
#
|
26
|
+
# @return [::Array]
|
27
|
+
# The coerced Array.
|
28
|
+
#
|
29
|
+
def self.coerce(value)
|
30
|
+
if value.respond_to?(:to_a)
|
31
|
+
value.to_a
|
32
|
+
elsif value.respond_to?(:to_ary)
|
33
|
+
value.to_ary
|
34
|
+
else
|
35
|
+
[value]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
#
|
40
|
+
# The Ruby Type for the Array Type instance.
|
41
|
+
#
|
42
|
+
# @return [Array<Class>]
|
43
|
+
# A singleton Array containing the element-type.
|
44
|
+
#
|
45
|
+
def to_ruby
|
46
|
+
self.class.to_ruby[@element_type.to_ruby]
|
47
|
+
end
|
48
|
+
|
49
|
+
#
|
50
|
+
# Determines if the value is an Array.
|
51
|
+
#
|
52
|
+
# @param [::Object] value
|
53
|
+
# The value to inspect.
|
54
|
+
#
|
55
|
+
# @return [::Boolean]
|
56
|
+
#
|
57
|
+
def ===(value)
|
58
|
+
(self.class === value) && value.all? { |element|
|
59
|
+
@element_type === element
|
60
|
+
}
|
61
|
+
end
|
62
|
+
|
63
|
+
#
|
64
|
+
# Coerces a value into an Array, and coerces the elements of the Array.
|
65
|
+
#
|
66
|
+
# @param [#to_a, ::Object] value
|
67
|
+
# The value to coerce.
|
68
|
+
#
|
69
|
+
# @return [::Array]
|
70
|
+
# The coerced Array.
|
71
|
+
#
|
72
|
+
# @see coerce
|
73
|
+
#
|
74
|
+
def coerce(value)
|
75
|
+
array = super(value)
|
76
|
+
array.map! { |element| @element_type.coerce(element) }
|
77
|
+
|
78
|
+
return array
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'parameters/types/object'
|
2
|
+
|
3
|
+
module Parameters
|
4
|
+
module Types
|
5
|
+
class Boolean < Type
|
6
|
+
|
7
|
+
#
|
8
|
+
# @return [true]
|
9
|
+
#
|
10
|
+
def self.type
|
11
|
+
true
|
12
|
+
end
|
13
|
+
|
14
|
+
#
|
15
|
+
# Determine if the value is a Boolean.
|
16
|
+
#
|
17
|
+
# @param [true, false] value
|
18
|
+
# The value to inspect.
|
19
|
+
#
|
20
|
+
# @return [::Boolean]
|
21
|
+
# Specifies whether the value was a Boolean.
|
22
|
+
#
|
23
|
+
def self.===(value)
|
24
|
+
(value == true) || (value == false)
|
25
|
+
end
|
26
|
+
|
27
|
+
#
|
28
|
+
# Coerces the value into a Boolean.
|
29
|
+
#
|
30
|
+
# @param [true, false, ::String, ::Symbol, nil] value
|
31
|
+
# The value to coerce.
|
32
|
+
#
|
33
|
+
# @return [true, false]
|
34
|
+
# The Boolean value.
|
35
|
+
#
|
36
|
+
def self.coerce(value)
|
37
|
+
case value
|
38
|
+
when FalseClass, NilClass, 'false', :false
|
39
|
+
false
|
40
|
+
else
|
41
|
+
true
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'parameters/types/object'
|
2
|
+
|
3
|
+
module Parameters
|
4
|
+
module Types
|
5
|
+
class Class < Object
|
6
|
+
|
7
|
+
# The base-class of the Class Type
|
8
|
+
attr_reader :base_class
|
9
|
+
|
10
|
+
#
|
11
|
+
# Initializes the Class Type.
|
12
|
+
#
|
13
|
+
# @param [Class] base_class
|
14
|
+
# The base-class to wrap all values within.
|
15
|
+
#
|
16
|
+
def initialize(base_class)
|
17
|
+
@base_class = base_class
|
18
|
+
end
|
19
|
+
|
20
|
+
#
|
21
|
+
# The Ruby Class the type represents.
|
22
|
+
#
|
23
|
+
# @return [Class]
|
24
|
+
# The base-class of the Class Type.
|
25
|
+
#
|
26
|
+
def to_ruby
|
27
|
+
@base_class
|
28
|
+
end
|
29
|
+
|
30
|
+
#
|
31
|
+
# Coerces a value into an instance of the Class.
|
32
|
+
#
|
33
|
+
# @param [Object] value
|
34
|
+
# The value to coerce.
|
35
|
+
#
|
36
|
+
# @return [Object]
|
37
|
+
# The instance of the Class, created using the value.
|
38
|
+
#
|
39
|
+
def coerce(value)
|
40
|
+
@base_class.new(value)
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'parameters/types/object'
|
2
|
+
|
3
|
+
require 'date'
|
4
|
+
|
5
|
+
module Parameters
|
6
|
+
module Types
|
7
|
+
class Date < Object
|
8
|
+
|
9
|
+
#
|
10
|
+
# Coerces a value into a Date.
|
11
|
+
#
|
12
|
+
# @param [::String, #to_date] value
|
13
|
+
# The value to coerce.
|
14
|
+
#
|
15
|
+
# @return [::Date]
|
16
|
+
# The coerced Date.
|
17
|
+
#
|
18
|
+
def self.coerce(value)
|
19
|
+
if value.respond_to?(:to_date)
|
20
|
+
value.to_date
|
21
|
+
else
|
22
|
+
::Date.parse(value.to_s)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'parameters/types/date'
|
2
|
+
|
3
|
+
require 'date'
|
4
|
+
require 'time'
|
5
|
+
|
6
|
+
module Parameters
|
7
|
+
module Types
|
8
|
+
class DateTime < Date
|
9
|
+
|
10
|
+
#
|
11
|
+
# Coerces a value into a DateTime object.
|
12
|
+
#
|
13
|
+
# @param [#to_datetime, ::String] value
|
14
|
+
# The value to coerce.
|
15
|
+
#
|
16
|
+
# @return [::DateTime]
|
17
|
+
# The coerced DateTime.
|
18
|
+
#
|
19
|
+
def self.coerce(value)
|
20
|
+
if value.respond_to?(:to_datetime)
|
21
|
+
value.to_datetime
|
22
|
+
else
|
23
|
+
::DateTime.parse(value.to_s)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'parameters/types/object'
|
2
|
+
|
3
|
+
module Parameters
|
4
|
+
module Types
|
5
|
+
class Float < Object
|
6
|
+
|
7
|
+
#
|
8
|
+
# The coerces a value into a Float.
|
9
|
+
#
|
10
|
+
# @param [#to_f, ::String] value
|
11
|
+
# The value to coerce.
|
12
|
+
#
|
13
|
+
# @return [::Float]
|
14
|
+
# The coerced Float.
|
15
|
+
#
|
16
|
+
def self.coerce(value)
|
17
|
+
if value.respond_to?(:to_f)
|
18
|
+
value.to_f
|
19
|
+
else
|
20
|
+
0.0
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
require 'parameters/types/object'
|
2
|
+
|
3
|
+
module Parameters
|
4
|
+
module Types
|
5
|
+
class Hash < Object
|
6
|
+
|
7
|
+
# The type to apply to all keys
|
8
|
+
attr_reader :key_type
|
9
|
+
|
10
|
+
# The type to apply to all values
|
11
|
+
attr_reader :value_type
|
12
|
+
|
13
|
+
#
|
14
|
+
# @param [Type] key_type
|
15
|
+
#
|
16
|
+
# @param [Type] value_type
|
17
|
+
#
|
18
|
+
def initialize(key_type,value_type)
|
19
|
+
@key_type = key_type
|
20
|
+
@value_type = value_type
|
21
|
+
end
|
22
|
+
|
23
|
+
#
|
24
|
+
# Coerces a value into a Hash.
|
25
|
+
#
|
26
|
+
# @param [::Array, #to_hash, ::Object] value
|
27
|
+
# The value to coerce.
|
28
|
+
#
|
29
|
+
# @return [::Hash]
|
30
|
+
# The coerced Hash.
|
31
|
+
#
|
32
|
+
def self.coerce(value)
|
33
|
+
case value
|
34
|
+
when ::Hash
|
35
|
+
value
|
36
|
+
when ::Array
|
37
|
+
::Hash[*value]
|
38
|
+
else
|
39
|
+
if value.respond_to?(:to_hash)
|
40
|
+
value.to_hash
|
41
|
+
else
|
42
|
+
raise(TypeError,"cannot coerce #{value.inspect} into a Hash")
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
#
|
48
|
+
# The Ruby Type that the Hash Type instance represents.
|
49
|
+
#
|
50
|
+
# @return [Hash{Class => Class}]
|
51
|
+
# A singleton Hash containing the key and value types.
|
52
|
+
#
|
53
|
+
def to_ruby
|
54
|
+
::Hash[@key_type.to_ruby => @value_type.to_ruby]
|
55
|
+
end
|
56
|
+
|
57
|
+
#
|
58
|
+
# Determines if the Hash, and all keys/values, are related to the Type.
|
59
|
+
#
|
60
|
+
# @param [Object, Hash] value
|
61
|
+
# The value to inspect.
|
62
|
+
#
|
63
|
+
# @return [Boolean]
|
64
|
+
# Specifies whether the Hash, and all keys/values, match the Type.
|
65
|
+
#
|
66
|
+
def ===(value)
|
67
|
+
(self.class === value) && value.entries.all? do |k,v|
|
68
|
+
(@key_type.nil? || @key_type === k) &&
|
69
|
+
(@value_type.nil? || @value_type === v)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
#
|
74
|
+
# Coerces a value into a Hash, and coerces the keys/values of the Hash.
|
75
|
+
#
|
76
|
+
# @param [::Array, #to_hash, ::Object] value
|
77
|
+
# The value to coerce.
|
78
|
+
#
|
79
|
+
# @return [::Hash]
|
80
|
+
# The coerced Hash.
|
81
|
+
#
|
82
|
+
def coerce(value)
|
83
|
+
hash = super(value)
|
84
|
+
coerced_hash = {}
|
85
|
+
|
86
|
+
hash.each do |k,v|
|
87
|
+
k = @key_type.coerce(k) if @key_type
|
88
|
+
v = @value_type.coerce(v) if @value_type
|
89
|
+
|
90
|
+
coerced_hash[k] = v
|
91
|
+
end
|
92
|
+
|
93
|
+
return coerced_hash
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|