parameters 0.2.3 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- 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/.gemtest
ADDED
File without changes
|
data/.gitignore
ADDED
data/ChangeLog.md
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
### 0.3.0 / 2011-12-13
|
2
|
+
|
3
|
+
* Added {Parameters::ClassMethods#set_param}.
|
4
|
+
* Added {Parameters#set_param}.
|
5
|
+
* Added {Parameters::Types}.
|
6
|
+
* Improved the type coercion logic (via {Parameters::Types}).
|
7
|
+
|
1
8
|
### 0.2.3 / 2010-10-27
|
2
9
|
|
3
10
|
* Have parameter reader/writer class-methods search down the ancestory
|
data/LICENSE.txt
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
|
2
|
-
Copyright (c) 2008-
|
2
|
+
Copyright (c) 2008-2011 Hal Brodigan
|
3
3
|
|
4
4
|
Permission is hereby granted, free of charge, to any person obtaining
|
5
5
|
a copy of this software and associated documentation files (the
|
@@ -19,4 +19,3 @@ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
19
19
|
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
20
20
|
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
21
21
|
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
-
|
data/README.md
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
# Parameters
|
2
2
|
|
3
|
-
* [
|
4
|
-
* [
|
5
|
-
*
|
3
|
+
* [Source](http://github.com/postmodern/parameters)
|
4
|
+
* [Issues](http://github.com/postmodern/parameters/issues)
|
5
|
+
* [Documentation](http://rubydoc.info/gems/parameters/frames)
|
6
|
+
* [Email](mailto:postmodern.mod3 at gmail.com)
|
6
7
|
|
7
8
|
## Description
|
8
9
|
|
@@ -21,6 +22,8 @@ have configurable default values.
|
|
21
22
|
|
22
23
|
## Examples
|
23
24
|
|
25
|
+
require 'parameters'
|
26
|
+
|
24
27
|
class Octagon
|
25
28
|
|
26
29
|
include Parameters
|
@@ -42,28 +45,26 @@ have configurable default values.
|
|
42
45
|
# URI, Regexp, DateTime, Date, Symbol, String, Integer, Float
|
43
46
|
# and true (for boolean types).
|
44
47
|
#
|
45
|
-
parameter :radius,
|
46
|
-
|
47
|
-
:description => 'The radius of the Octagon'
|
48
|
+
parameter :radius, :type => Float,
|
49
|
+
:description => 'The radius of the Octagon'
|
48
50
|
|
49
51
|
#
|
50
52
|
# A parameter with a lambda for a default value
|
51
53
|
#
|
52
|
-
parameter :opacity,
|
53
|
-
|
54
|
-
:description => 'The opacity of the Octagon'
|
54
|
+
parameter :opacity, :default => lambda { rand },
|
55
|
+
:description => 'The opacity of the Octagon'
|
55
56
|
|
56
57
|
end
|
57
58
|
|
58
59
|
# Create an object with default values for all parameters
|
59
60
|
oct = Octagon.new
|
60
|
-
oct.x
|
61
|
-
oct.y
|
61
|
+
oct.x # => 0
|
62
|
+
oct.y # => 0.5
|
62
63
|
oct.opacity # => 0.25
|
63
64
|
|
64
65
|
# Create an object with the given parameter values.
|
65
66
|
oct = Octagon.new(:radius => 10)
|
66
|
-
oct.radius
|
67
|
+
oct.radius # => 10
|
67
68
|
oct.opacity # => 0.7
|
68
69
|
|
69
70
|
# Set parameter values of a class
|
@@ -73,7 +74,7 @@ have configurable default values.
|
|
73
74
|
# Create an object with parameter defaulte values inherited from the
|
74
75
|
# class parameters
|
75
76
|
oct = Octagon.new
|
76
|
-
oct.radius
|
77
|
+
oct.radius # => 33
|
77
78
|
oct.opacity # => 0.3
|
78
79
|
|
79
80
|
# Coerce data from the command-line into the given parameter type
|
@@ -82,7 +83,7 @@ have configurable default values.
|
|
82
83
|
|
83
84
|
## Install
|
84
85
|
|
85
|
-
$
|
86
|
+
$ gem install parameters
|
86
87
|
|
87
88
|
## License
|
88
89
|
|
data/Rakefile
CHANGED
@@ -2,17 +2,17 @@ require 'rubygems'
|
|
2
2
|
require 'rake'
|
3
3
|
|
4
4
|
begin
|
5
|
-
gem 'ore-tasks', '~> 0.
|
5
|
+
gem 'ore-tasks', '~> 0.4'
|
6
6
|
require 'ore/tasks'
|
7
7
|
|
8
8
|
Ore::Tasks.new
|
9
9
|
rescue LoadError => e
|
10
|
-
|
11
|
-
|
10
|
+
warn e.message
|
11
|
+
warn "Run `gem install ore-tasks` to install 'ore/tasks'."
|
12
12
|
end
|
13
13
|
|
14
14
|
begin
|
15
|
-
gem 'rspec', '~> 2.
|
15
|
+
gem 'rspec', '~> 2.4'
|
16
16
|
require 'rspec/core/rake_task'
|
17
17
|
|
18
18
|
RSpec::Core::RakeTask.new
|
@@ -21,10 +21,11 @@ rescue LoadError => e
|
|
21
21
|
abort "Please run `gem install rspec` to install RSpec."
|
22
22
|
end
|
23
23
|
end
|
24
|
+
task :test => :spec
|
24
25
|
task :default => :spec
|
25
26
|
|
26
27
|
begin
|
27
|
-
gem 'yard', '~> 0.
|
28
|
+
gem 'yard', '~> 0.7'
|
28
29
|
require 'yard'
|
29
30
|
|
30
31
|
YARD::Rake::YardocTask.new
|
data/gemspec.yml
CHANGED
@@ -10,8 +10,9 @@ email: postmodern.mod3@gmail.com
|
|
10
10
|
homepage: http://github.com/postmodern/parameters
|
11
11
|
has_yard: true
|
12
12
|
|
13
|
+
required_ruby_version: ">= 1.8.7"
|
14
|
+
|
13
15
|
development_dependencies:
|
14
|
-
ore: ~> 0.
|
15
|
-
|
16
|
-
|
17
|
-
yard: ~> 0.6.0
|
16
|
+
ore-tasks: ~> 0.4
|
17
|
+
rspec: ~> 2.4
|
18
|
+
yard: ~> 0.7
|
@@ -29,11 +29,13 @@ module Parameters
|
|
29
29
|
def params=(values)
|
30
30
|
values.each do |name,value|
|
31
31
|
if has_param?(name)
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
32
|
+
get_param(name).value = case value
|
33
|
+
when Parameters::ClassParam,
|
34
|
+
Parameters::InstanceParam
|
35
|
+
value.value
|
36
|
+
else
|
37
|
+
value
|
38
|
+
end
|
37
39
|
end
|
38
40
|
end
|
39
41
|
end
|
@@ -65,14 +67,6 @@ module Parameters
|
|
65
67
|
def parameter(name,options={})
|
66
68
|
name = name.to_sym
|
67
69
|
|
68
|
-
# add the parameter to the class params list
|
69
|
-
params[name] = Parameters::ClassParam.new(
|
70
|
-
name,
|
71
|
-
options[:type],
|
72
|
-
options[:description],
|
73
|
-
options[:default]
|
74
|
-
)
|
75
|
-
|
76
70
|
# define the reader class method for the parameter
|
77
71
|
meta_def(name) do
|
78
72
|
get_param(name).value
|
@@ -85,6 +79,37 @@ module Parameters
|
|
85
79
|
|
86
80
|
# define the getter/setter instance methods for the parameter
|
87
81
|
attr_accessor(name)
|
82
|
+
|
83
|
+
# create the new parameter
|
84
|
+
new_param = Parameters::ClassParam.new(
|
85
|
+
name,
|
86
|
+
options[:type],
|
87
|
+
options[:description],
|
88
|
+
options[:default]
|
89
|
+
)
|
90
|
+
|
91
|
+
# add the parameter to the class params list
|
92
|
+
params[name] = new_param
|
93
|
+
return new_param
|
94
|
+
end
|
95
|
+
|
96
|
+
#
|
97
|
+
# Determines if a class parameter exists with the given name.
|
98
|
+
#
|
99
|
+
# @return [Boolean]
|
100
|
+
# Specifies whether or not there is a class parameter with the
|
101
|
+
# specified name.
|
102
|
+
#
|
103
|
+
def has_param?(name)
|
104
|
+
name = name.to_sym
|
105
|
+
|
106
|
+
ancestors.each do |ancestor|
|
107
|
+
if ancestor.included_modules.include?(Parameters)
|
108
|
+
return true if ancestor.params.has_key?(name)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
return false
|
88
113
|
end
|
89
114
|
|
90
115
|
#
|
@@ -94,10 +119,10 @@ module Parameters
|
|
94
119
|
# The class parameter name to search for.
|
95
120
|
#
|
96
121
|
# @return [ClassParam]
|
97
|
-
# The class parameter with the matching
|
122
|
+
# The class parameter with the matching name.
|
98
123
|
#
|
99
124
|
# @raise [ParamNotFound]
|
100
|
-
# No class parameter with the specified
|
125
|
+
# No class parameter with the specified name could be found.
|
101
126
|
#
|
102
127
|
def get_param(name)
|
103
128
|
name = name.to_sym
|
@@ -110,24 +135,38 @@ module Parameters
|
|
110
135
|
end
|
111
136
|
end
|
112
137
|
|
113
|
-
raise(Parameters::ParamNotFound,"parameter #{name.to_s.dump} was not found in class #{self
|
138
|
+
raise(Parameters::ParamNotFound,"parameter #{name.to_s.dump} was not found in class #{self}")
|
114
139
|
end
|
115
140
|
|
116
141
|
#
|
117
|
-
#
|
118
|
-
# Specifies whether or not there is a class parameter with the
|
119
|
-
# specified _name_.
|
142
|
+
# Sets a class parameter.
|
120
143
|
#
|
121
|
-
|
144
|
+
# @param [Symbol, String] name
|
145
|
+
# The name of the class parameter.
|
146
|
+
#
|
147
|
+
# @param [Object] value
|
148
|
+
# The new value for the class parameter.
|
149
|
+
#
|
150
|
+
# @return [Object]
|
151
|
+
# The new value of the class parameter.
|
152
|
+
#
|
153
|
+
# @raise [ParamNotfound]
|
154
|
+
# No class parameter with the specified name could be found.
|
155
|
+
#
|
156
|
+
# @since 0.3.0
|
157
|
+
#
|
158
|
+
def set_param(name,value)
|
122
159
|
name = name.to_sym
|
123
160
|
|
124
161
|
ancestors.each do |ancestor|
|
125
162
|
if ancestor.included_modules.include?(Parameters)
|
126
|
-
|
163
|
+
if ancestor.params.has_key?(name)
|
164
|
+
return ancestor.params[name].set(value)
|
165
|
+
end
|
127
166
|
end
|
128
167
|
end
|
129
168
|
|
130
|
-
|
169
|
+
raise(Parameters::ParamNotFound,"parameter #{name.to_s.dump} was not found in class #{self}")
|
131
170
|
end
|
132
171
|
|
133
172
|
#
|
@@ -42,6 +42,29 @@ module Parameters
|
|
42
42
|
@value = coerce(new_value)
|
43
43
|
end
|
44
44
|
|
45
|
+
#
|
46
|
+
# Creates an instance parameter from the class param.
|
47
|
+
#
|
48
|
+
# @param [Object] object
|
49
|
+
# The object the instance parameter should be connected to.
|
50
|
+
#
|
51
|
+
# @return [InstanceParam]
|
52
|
+
# The new instance parameter.
|
53
|
+
#
|
54
|
+
# @since 0.3.0
|
55
|
+
#
|
56
|
+
# @api semipublic
|
57
|
+
#
|
58
|
+
def to_instance(object)
|
59
|
+
InstanceParam.new(
|
60
|
+
object,
|
61
|
+
@name,
|
62
|
+
@type,
|
63
|
+
@description,
|
64
|
+
@value
|
65
|
+
)
|
66
|
+
end
|
67
|
+
|
45
68
|
#
|
46
69
|
# @return [String]
|
47
70
|
# The representation of the class param.
|
@@ -22,10 +22,30 @@ module Parameters
|
|
22
22
|
# @param [String, nil] description
|
23
23
|
# The description of the instance parameter.
|
24
24
|
#
|
25
|
-
|
25
|
+
# @param [Object] value
|
26
|
+
# The initial value for the instance parameter.
|
27
|
+
#
|
28
|
+
def initialize(object,name,type=nil,description=nil,value=nil)
|
26
29
|
super(name,type,description)
|
27
30
|
|
28
31
|
@object = object
|
32
|
+
|
33
|
+
if (self.value.nil? && value)
|
34
|
+
self.value = case value
|
35
|
+
when Proc
|
36
|
+
if value.arity > 0
|
37
|
+
value.call(@object)
|
38
|
+
else
|
39
|
+
value.call()
|
40
|
+
end
|
41
|
+
else
|
42
|
+
begin
|
43
|
+
value.clone
|
44
|
+
rescue TypeError
|
45
|
+
value
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
29
49
|
end
|
30
50
|
|
31
51
|
#
|
data/lib/parameters/param.rb
CHANGED
@@ -1,6 +1,4 @@
|
|
1
|
-
require '
|
2
|
-
require 'uri'
|
3
|
-
require 'date'
|
1
|
+
require 'parameters/types'
|
4
2
|
|
5
3
|
module Parameters
|
6
4
|
class Param
|
@@ -28,79 +26,26 @@ module Parameters
|
|
28
26
|
#
|
29
27
|
def initialize(name,type=nil,description=nil)
|
30
28
|
@name = name.to_sym
|
31
|
-
@type = type
|
29
|
+
@type = case type
|
30
|
+
when Types::Type
|
31
|
+
type
|
32
|
+
when Class
|
33
|
+
if type < Types::Type
|
34
|
+
type
|
35
|
+
else
|
36
|
+
Types[type]
|
37
|
+
end
|
38
|
+
else
|
39
|
+
Types[type]
|
40
|
+
end
|
41
|
+
|
32
42
|
@description = description
|
33
43
|
end
|
34
44
|
|
35
45
|
protected
|
36
46
|
|
37
|
-
# Type classes and their coercion methods
|
38
|
-
TYPE_COERSION = {
|
39
|
-
Hash => :coerce_hash,
|
40
|
-
Set => :coerce_set,
|
41
|
-
Array => :coerce_array,
|
42
|
-
URI => :coerce_uri,
|
43
|
-
Regexp => :coerce_regexp,
|
44
|
-
DateTime => :coerce_date,
|
45
|
-
Date => :coerce_date,
|
46
|
-
Symbol => :coerce_symbol,
|
47
|
-
String => :coerce_string,
|
48
|
-
Integer => :coerce_integer,
|
49
|
-
Float => :coerce_float,
|
50
|
-
true => :coerce_boolean
|
51
|
-
}
|
52
|
-
|
53
|
-
#
|
54
|
-
# Coerces a given value into a specific type.
|
55
|
-
#
|
56
|
-
# @param [Class, Proc] type
|
57
|
-
# The type to coerce the value into. If a Proc is given, it will be
|
58
|
-
# called with the value to coerce.
|
59
|
-
#
|
60
|
-
# @param [Object] value
|
61
|
-
# The value to coerce.
|
62
|
-
#
|
63
|
-
# @return [Object]
|
64
|
-
# The coerced value.
|
65
|
-
#
|
66
|
-
# @since 0.2.0
|
67
|
-
#
|
68
|
-
def coerce_type(type,value)
|
69
|
-
if value.nil?
|
70
|
-
nil
|
71
|
-
elsif type.kind_of?(Hash)
|
72
|
-
key_type, value_type = type.entries.first
|
73
|
-
new_hash = {}
|
74
|
-
|
75
|
-
coerce_hash(Hash,value).each do |key,value|
|
76
|
-
key = coerce_type(key_type,key)
|
77
|
-
value = coerce_type(value_type,value)
|
78
|
-
|
79
|
-
new_hash[key] = value
|
80
|
-
end
|
81
|
-
|
82
|
-
return new_hash
|
83
|
-
elsif type.kind_of?(Set)
|
84
|
-
coerce_array(Array,value).map { |element|
|
85
|
-
coerce_type(type.entries.first,element)
|
86
|
-
}.to_set
|
87
|
-
elsif type.kind_of?(Array)
|
88
|
-
coerce_array(Array,value).map do |element|
|
89
|
-
coerce_type(type.entries.first,element)
|
90
|
-
end
|
91
|
-
elsif type.kind_of?(Proc)
|
92
|
-
type.call(value)
|
93
|
-
elsif (method_name = TYPE_COERSION[type])
|
94
|
-
self.send(method_name,type,value)
|
95
|
-
elsif (type.nil? || type == Object)
|
96
|
-
value
|
97
|
-
else
|
98
|
-
type.new(value)
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
47
|
#
|
103
|
-
# Coerces
|
48
|
+
# Coerces the value into the param type.
|
104
49
|
#
|
105
50
|
# @param [Object] value
|
106
51
|
# The value to coerce.
|
@@ -108,276 +53,11 @@ module Parameters
|
|
108
53
|
# @return [Object]
|
109
54
|
# The coerced value.
|
110
55
|
#
|
111
|
-
# @since 0.2.0
|
112
|
-
#
|
113
56
|
def coerce(value)
|
114
|
-
|
115
|
-
end
|
116
|
-
|
117
|
-
#
|
118
|
-
# Coerces a given value into a `Hash`.
|
119
|
-
#
|
120
|
-
# @param [Hash{Class => Class}] type
|
121
|
-
# An optional `Set` containing the type to coerce the keys and values
|
122
|
-
# of the given value to.
|
123
|
-
#
|
124
|
-
# @param [Hash, #to_hash, Object] value
|
125
|
-
# The value to coerce into a `Hash`.
|
126
|
-
#
|
127
|
-
# @return [Hash]
|
128
|
-
# The coerced value.
|
129
|
-
#
|
130
|
-
# @since 0.2.1
|
131
|
-
#
|
132
|
-
def coerce_hash(type,value)
|
133
|
-
if value.kind_of?(Hash)
|
57
|
+
if @type === value
|
134
58
|
value
|
135
|
-
elsif value.kind_of?(Array)
|
136
|
-
Hash[*value]
|
137
|
-
elsif value.respond_to?(:to_hash)
|
138
|
-
value.to_hash
|
139
|
-
else
|
140
|
-
{value => true}
|
141
|
-
end
|
142
|
-
end
|
143
|
-
|
144
|
-
#
|
145
|
-
# Coerces a given value into a `Set`.
|
146
|
-
#
|
147
|
-
# @param [Set[Class]] type
|
148
|
-
# An optional `Set` containing the type to coerce the elements
|
149
|
-
# of the given value to.
|
150
|
-
#
|
151
|
-
# @param [Enumerable, Object] value
|
152
|
-
# The value to coerce into a `Set`.
|
153
|
-
#
|
154
|
-
# @return [Set]
|
155
|
-
# The coerced value.
|
156
|
-
#
|
157
|
-
# @since 0.2.0
|
158
|
-
#
|
159
|
-
def coerce_set(type,value)
|
160
|
-
if value.kind_of?(Set)
|
161
|
-
value
|
162
|
-
elsif (value.kind_of?(Enumerable) || value.respond_to?(:to_set))
|
163
|
-
value.to_set
|
164
|
-
else
|
165
|
-
Set[value]
|
166
|
-
end
|
167
|
-
end
|
168
|
-
|
169
|
-
#
|
170
|
-
# Coerces a given value into an `Array`.
|
171
|
-
#
|
172
|
-
# @param [Array[Class]] type
|
173
|
-
# An optional `Array` containing the type to coerce the elements
|
174
|
-
# of the given value to.
|
175
|
-
#
|
176
|
-
# @param [Enumerable, Object] value
|
177
|
-
# The value to coerce into an `Array`.
|
178
|
-
#
|
179
|
-
# @return [Array]
|
180
|
-
# The coerced value.
|
181
|
-
#
|
182
|
-
# @since 0.2.0
|
183
|
-
#
|
184
|
-
def coerce_array(type,value)
|
185
|
-
if value.kind_of?(Array)
|
186
|
-
value
|
187
|
-
elsif (value.kind_of?(Enumerable) || value.respond_to?(:to_a))
|
188
|
-
value.to_a
|
189
|
-
else
|
190
|
-
[value]
|
191
|
-
end
|
192
|
-
end
|
193
|
-
|
194
|
-
#
|
195
|
-
# Coerces a given value into a `URI`.
|
196
|
-
#
|
197
|
-
# @param [Class] type
|
198
|
-
# The `URI` type to coerce to.
|
199
|
-
#
|
200
|
-
# @param [URI::Generic, #to_s] value
|
201
|
-
# The value to coerce into a `URI`.
|
202
|
-
#
|
203
|
-
# @return [URI::Generic]
|
204
|
-
# The coerced value.
|
205
|
-
#
|
206
|
-
# @since 0.2.0
|
207
|
-
#
|
208
|
-
def coerce_uri(type,value)
|
209
|
-
if value.kind_of?(type)
|
210
|
-
value
|
211
|
-
else
|
212
|
-
URI.parse(value.to_s)
|
213
|
-
end
|
214
|
-
end
|
215
|
-
|
216
|
-
#
|
217
|
-
# Coerces a given value into a `Regexp`.
|
218
|
-
#
|
219
|
-
# @param [Class] type
|
220
|
-
# The `Regexp` type to coerce to.
|
221
|
-
#
|
222
|
-
# @param [Regexp, #to_s] value
|
223
|
-
# The value to coerce into a `Regexp`.
|
224
|
-
#
|
225
|
-
# @return [Regexp]
|
226
|
-
# The coerced value.
|
227
|
-
#
|
228
|
-
# @since 0.2.0
|
229
|
-
#
|
230
|
-
def coerce_regexp(type,value)
|
231
|
-
if value.kind_of?(Regexp)
|
232
|
-
value
|
233
|
-
else
|
234
|
-
Regexp.new(value.to_s)
|
235
|
-
end
|
236
|
-
end
|
237
|
-
|
238
|
-
#
|
239
|
-
# Coerces a given value into a `Symbol`.
|
240
|
-
#
|
241
|
-
# @param [Class] type
|
242
|
-
# The `Symbol` class.
|
243
|
-
#
|
244
|
-
# @param [#to_s] value
|
245
|
-
# The value to coerce.
|
246
|
-
#
|
247
|
-
# @return [Symbol]
|
248
|
-
# The coerced value.
|
249
|
-
#
|
250
|
-
# @since 0.2.0
|
251
|
-
#
|
252
|
-
def coerce_symbol(type,value)
|
253
|
-
if value.kind_of?(type)
|
254
|
-
value
|
255
|
-
else
|
256
|
-
value.to_s.to_sym
|
257
|
-
end
|
258
|
-
end
|
259
|
-
|
260
|
-
#
|
261
|
-
# Coerces a given value into a `String`.
|
262
|
-
#
|
263
|
-
# @param [Class] type
|
264
|
-
# The `String` class.
|
265
|
-
#
|
266
|
-
# @param [#to_s] value
|
267
|
-
# The value to coerce into a `String`.
|
268
|
-
#
|
269
|
-
# @return [String]
|
270
|
-
# The coerced value.
|
271
|
-
#
|
272
|
-
# @since 0.2.0
|
273
|
-
#
|
274
|
-
def coerce_string(type,value)
|
275
|
-
if value.kind_of?(type)
|
276
|
-
value
|
277
|
-
else
|
278
|
-
value.to_s
|
279
|
-
end
|
280
|
-
end
|
281
|
-
|
282
|
-
#
|
283
|
-
# Coerces a given value into an `Integer`.
|
284
|
-
#
|
285
|
-
# @param [Class]
|
286
|
-
# The Integer class.
|
287
|
-
#
|
288
|
-
# @param [String, #to_i] value
|
289
|
-
# The value to coerce into an `Integer`.
|
290
|
-
#
|
291
|
-
# @return [Integer]
|
292
|
-
# The coerced value.
|
293
|
-
#
|
294
|
-
# @since 0.2.0
|
295
|
-
#
|
296
|
-
def coerce_integer(type,value)
|
297
|
-
if value.kind_of?(type)
|
298
|
-
value
|
299
|
-
elsif value.kind_of?(String)
|
300
|
-
base = if value[0..1] == '0x'
|
301
|
-
16
|
302
|
-
elsif value[0..0] == '0'
|
303
|
-
8
|
304
|
-
else
|
305
|
-
10
|
306
|
-
end
|
307
|
-
|
308
|
-
value.to_i(base)
|
309
|
-
elsif value.respond_to?(:to_i)
|
310
|
-
value.to_i
|
311
|
-
else
|
312
|
-
0
|
313
|
-
end
|
314
|
-
end
|
315
|
-
|
316
|
-
#
|
317
|
-
# Coerces a given value into a `Float`.
|
318
|
-
#
|
319
|
-
# @param [Class] type
|
320
|
-
# The `Float` class.
|
321
|
-
#
|
322
|
-
# @param [String, #to_f] value
|
323
|
-
# The value to coerce into a `Float`.
|
324
|
-
#
|
325
|
-
# @return [Float]
|
326
|
-
# The coerced value.
|
327
|
-
#
|
328
|
-
# @since 0.2.0
|
329
|
-
#
|
330
|
-
def coerce_float(type,value)
|
331
|
-
if value.kind_of?(type)
|
332
|
-
value
|
333
|
-
elsif (value.kind_of?(String) || value.respond_to?(:to_f))
|
334
|
-
value.to_f
|
335
|
-
else
|
336
|
-
0.0
|
337
|
-
end
|
338
|
-
end
|
339
|
-
|
340
|
-
#
|
341
|
-
# Coerces a given value into a `DateTime` or `Date`.
|
342
|
-
#
|
343
|
-
# @param [Class] type
|
344
|
-
# The `DateTime` or `Date` class.
|
345
|
-
#
|
346
|
-
# @param [#to_s] value
|
347
|
-
# The value to coerce into either a `Date` or `DateTime` object.
|
348
|
-
#
|
349
|
-
# @return [DateTime, Date]
|
350
|
-
# The coerced value.
|
351
|
-
#
|
352
|
-
# @since 0.2.0
|
353
|
-
#
|
354
|
-
def coerce_date(type,value)
|
355
|
-
if value.kind_of?(type)
|
356
|
-
value
|
357
|
-
else
|
358
|
-
type.parse(value.to_s)
|
359
|
-
end
|
360
|
-
end
|
361
|
-
|
362
|
-
#
|
363
|
-
# Coerces a given value into either a `true` or `false` value.
|
364
|
-
#
|
365
|
-
# @param [true] type
|
366
|
-
#
|
367
|
-
# @param [TrueClass, FalseClass, String, Symbol] value
|
368
|
-
# The value to coerce into either a `true` or `false` value.
|
369
|
-
#
|
370
|
-
# @return [TrueClass, FalseClass]
|
371
|
-
# The coerced value.
|
372
|
-
#
|
373
|
-
# @since 0.2.0
|
374
|
-
#
|
375
|
-
def coerce_boolean(type,value)
|
376
|
-
case value
|
377
|
-
when FalseClass, NilClass, 'false', :false
|
378
|
-
false
|
379
59
|
else
|
380
|
-
|
60
|
+
@type.coerce(value)
|
381
61
|
end
|
382
62
|
end
|
383
63
|
|