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
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'parameters/types/object'
|
2
|
+
|
3
|
+
module Parameters
|
4
|
+
module Types
|
5
|
+
class Integer < Object
|
6
|
+
|
7
|
+
#
|
8
|
+
# Coerces a value into an Integer.
|
9
|
+
#
|
10
|
+
# @param [::String, #to_i] value
|
11
|
+
# The value to coerce.
|
12
|
+
#
|
13
|
+
# @return [::Integer]
|
14
|
+
# The coerced Integer.
|
15
|
+
#
|
16
|
+
def self.coerce(value)
|
17
|
+
case value
|
18
|
+
when ::String
|
19
|
+
value.to_i(0)
|
20
|
+
else
|
21
|
+
if value.respond_to?(:to_i)
|
22
|
+
value.to_i
|
23
|
+
else
|
24
|
+
0
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'parameters/types/type'
|
2
|
+
|
3
|
+
module Parameters
|
4
|
+
module Types
|
5
|
+
class Object < Type
|
6
|
+
|
7
|
+
#
|
8
|
+
# The Ruby Class the Type represents.
|
9
|
+
#
|
10
|
+
# @return [Class]
|
11
|
+
# The Ruby Class that matches the Types name.
|
12
|
+
#
|
13
|
+
def self.to_ruby
|
14
|
+
@ruby_class ||= ::Object.const_get(self.name.split('::').last)
|
15
|
+
end
|
16
|
+
|
17
|
+
#
|
18
|
+
# Determines if the value is an Object.
|
19
|
+
#
|
20
|
+
# @return [true]
|
21
|
+
#
|
22
|
+
def self.===(value)
|
23
|
+
value.kind_of?(to_ruby)
|
24
|
+
end
|
25
|
+
|
26
|
+
#
|
27
|
+
# Coerces the value into an Object.
|
28
|
+
#
|
29
|
+
# @param [::Object] value
|
30
|
+
# The value to coerce.
|
31
|
+
#
|
32
|
+
# @return [value]
|
33
|
+
# Passes through the value.
|
34
|
+
#
|
35
|
+
def self.coerce(value)
|
36
|
+
value
|
37
|
+
end
|
38
|
+
|
39
|
+
#
|
40
|
+
# Determines if the value is an Object.
|
41
|
+
#
|
42
|
+
# @return [true]
|
43
|
+
#
|
44
|
+
def ===(value)
|
45
|
+
value.kind_of?(to_ruby)
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'parameters/types/type'
|
2
|
+
|
3
|
+
module Parameters
|
4
|
+
module Types
|
5
|
+
class Proc < Type
|
6
|
+
|
7
|
+
# The callback that will coerce values
|
8
|
+
attr_reader :callback
|
9
|
+
|
10
|
+
#
|
11
|
+
# Creates a new Proc type.
|
12
|
+
#
|
13
|
+
# @param [#call] callback
|
14
|
+
# The callback that will handle the actual coercion.
|
15
|
+
#
|
16
|
+
def initialize(callback)
|
17
|
+
@callback = callback
|
18
|
+
end
|
19
|
+
|
20
|
+
#
|
21
|
+
# Coerces the value using the callback.
|
22
|
+
#
|
23
|
+
# @param [::Object] value
|
24
|
+
# The value to coerce.
|
25
|
+
#
|
26
|
+
# @return [::Object]
|
27
|
+
# The result of the callback.
|
28
|
+
#
|
29
|
+
def coerce(value)
|
30
|
+
@callback.call(value)
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'parameters/types/object'
|
2
|
+
|
3
|
+
module Parameters
|
4
|
+
module Types
|
5
|
+
class Regexp < Object
|
6
|
+
|
7
|
+
#
|
8
|
+
# Coerces a value into a Regular Expression.
|
9
|
+
#
|
10
|
+
# @param [#to_regexp, #to_s] value
|
11
|
+
# The value to coerce.
|
12
|
+
#
|
13
|
+
# @return [::Regexp]
|
14
|
+
# The coerced Regular Expression.
|
15
|
+
#
|
16
|
+
def self.coerce(value)
|
17
|
+
if value.respond_to?(:to_regexp)
|
18
|
+
value.to_regexp
|
19
|
+
else
|
20
|
+
::Regexp.new(value.to_s)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'parameters/types/array'
|
2
|
+
|
3
|
+
require 'set'
|
4
|
+
|
5
|
+
module Parameters
|
6
|
+
module Types
|
7
|
+
class Set < Array
|
8
|
+
|
9
|
+
#
|
10
|
+
# Coerces a value into a Set.
|
11
|
+
#
|
12
|
+
# @param [#to_set, ::Object] value
|
13
|
+
# The value to coerce.
|
14
|
+
#
|
15
|
+
# @return [::Set]
|
16
|
+
# The coerced Set.
|
17
|
+
#
|
18
|
+
def self.coerce(value)
|
19
|
+
if value.respond_to?(:to_set)
|
20
|
+
value.to_set
|
21
|
+
else
|
22
|
+
::Set[value]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'parameters/types/object'
|
2
|
+
|
3
|
+
module Parameters
|
4
|
+
module Types
|
5
|
+
class String < Object
|
6
|
+
|
7
|
+
#
|
8
|
+
# Coerces a value into a String.
|
9
|
+
#
|
10
|
+
# @param [#to_s] value
|
11
|
+
# The value to coerce.
|
12
|
+
#
|
13
|
+
# @return [::String]
|
14
|
+
# The coerced String.
|
15
|
+
#
|
16
|
+
def self.coerce(value)
|
17
|
+
value.to_s
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'parameters/types/object'
|
2
|
+
|
3
|
+
module Parameters
|
4
|
+
module Types
|
5
|
+
class Symbol < Object
|
6
|
+
|
7
|
+
#
|
8
|
+
# Coerces a value into a Symbol.
|
9
|
+
#
|
10
|
+
# @param [#to_sym, #to_s] value
|
11
|
+
# The value to coerce.
|
12
|
+
#
|
13
|
+
# @return [::Symbol]
|
14
|
+
# The coerced Symbol.
|
15
|
+
#
|
16
|
+
def self.coerce(value)
|
17
|
+
if value.respond_to?(:to_sym)
|
18
|
+
value.to_sym
|
19
|
+
else
|
20
|
+
value.to_s.to_sym
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'parameters/types/object'
|
2
|
+
|
3
|
+
require 'time'
|
4
|
+
|
5
|
+
module Parameters
|
6
|
+
module Types
|
7
|
+
class Time < Object
|
8
|
+
|
9
|
+
#
|
10
|
+
# Coerces a value into a Time object.
|
11
|
+
#
|
12
|
+
# @param [Integer, #to_time, #to_s] value
|
13
|
+
# The value to coerce.
|
14
|
+
#
|
15
|
+
# @return [::Time]
|
16
|
+
# The coerced Time object.
|
17
|
+
#
|
18
|
+
def self.coerce(value)
|
19
|
+
case value
|
20
|
+
when Integer
|
21
|
+
::Time.at(value)
|
22
|
+
else
|
23
|
+
if value.respond_to?(:to_time)
|
24
|
+
value.to_time
|
25
|
+
else
|
26
|
+
::Time.parse(value.to_s)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module Parameters
|
2
|
+
module Types
|
3
|
+
class Type
|
4
|
+
|
5
|
+
#
|
6
|
+
# The Ruby Class the type represents.
|
7
|
+
#
|
8
|
+
# @return [Class]
|
9
|
+
# A Ruby Class the Type represents.
|
10
|
+
#
|
11
|
+
# @abstract
|
12
|
+
#
|
13
|
+
def self.to_ruby
|
14
|
+
end
|
15
|
+
|
16
|
+
#
|
17
|
+
# @see to_ruby
|
18
|
+
#
|
19
|
+
def to_ruby
|
20
|
+
self.class.to_ruby
|
21
|
+
end
|
22
|
+
|
23
|
+
#
|
24
|
+
# Determines if the value is an instance of the Type.
|
25
|
+
#
|
26
|
+
# @return [Boolean]
|
27
|
+
# Specifies whether the value is already an instance of the Type.
|
28
|
+
#
|
29
|
+
# @abstract
|
30
|
+
#
|
31
|
+
def self.===(value)
|
32
|
+
false
|
33
|
+
end
|
34
|
+
|
35
|
+
#
|
36
|
+
# Coerces an Object into an instances of the Type.
|
37
|
+
#
|
38
|
+
# @param [Object] value
|
39
|
+
# The value to coerce.
|
40
|
+
#
|
41
|
+
# @return [Object]
|
42
|
+
# The coerced value.
|
43
|
+
#
|
44
|
+
# @abstract
|
45
|
+
#
|
46
|
+
def self.coerce(value)
|
47
|
+
end
|
48
|
+
|
49
|
+
#
|
50
|
+
# @see ===
|
51
|
+
#
|
52
|
+
def ===(value)
|
53
|
+
self.class === value
|
54
|
+
end
|
55
|
+
|
56
|
+
#
|
57
|
+
# @see coerce
|
58
|
+
#
|
59
|
+
def coerce(value)
|
60
|
+
self.class.coerce(value)
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
require 'parameters/types/hash'
|
2
|
+
require 'parameters/types/set'
|
3
|
+
require 'parameters/types/array'
|
4
|
+
require 'parameters/types/boolean'
|
5
|
+
require 'parameters/types/object'
|
6
|
+
require 'parameters/types/proc'
|
7
|
+
require 'parameters/types/class'
|
8
|
+
|
9
|
+
module Parameters
|
10
|
+
#
|
11
|
+
# @since 0.3.0
|
12
|
+
#
|
13
|
+
# @api private
|
14
|
+
#
|
15
|
+
module Types
|
16
|
+
#
|
17
|
+
# Determines if a Type is defined.
|
18
|
+
#
|
19
|
+
# @param [Symbol, String] name
|
20
|
+
# The name of the type.
|
21
|
+
#
|
22
|
+
# @return [Boolean]
|
23
|
+
# Specifies whether the type was defined within {Types}.
|
24
|
+
#
|
25
|
+
def self.type_defined?(name)
|
26
|
+
const_defined?(name) && (const_get(name) < Type)
|
27
|
+
end
|
28
|
+
|
29
|
+
#
|
30
|
+
# Looks up a Type.
|
31
|
+
#
|
32
|
+
# @param [Symbol, String] name
|
33
|
+
# The name of the type.
|
34
|
+
#
|
35
|
+
# @return [Type]
|
36
|
+
# The type within {Types}.
|
37
|
+
#
|
38
|
+
# @raise [NameError]
|
39
|
+
# The type could not be found.
|
40
|
+
#
|
41
|
+
def self.type_get(name)
|
42
|
+
type = const_get(name)
|
43
|
+
|
44
|
+
unless type < Type
|
45
|
+
raise(NameError,"unknown Parameter Type: #{name}")
|
46
|
+
end
|
47
|
+
|
48
|
+
return type
|
49
|
+
end
|
50
|
+
|
51
|
+
#
|
52
|
+
# Maps a Hash, Set, Array, Proc or Class to a Type.
|
53
|
+
#
|
54
|
+
# @param [Hash, Set, Array, Proc, Class] type
|
55
|
+
# The Ruby Class or Hash, Set, Array, Proc to map to a Type.
|
56
|
+
#
|
57
|
+
# @return [Types::Type]
|
58
|
+
# The mapped type.
|
59
|
+
#
|
60
|
+
# @raise [TypeError]
|
61
|
+
# The Ruby Type could not be mapped to a Parameter Type.
|
62
|
+
#
|
63
|
+
def self.[](type)
|
64
|
+
case type
|
65
|
+
when ::Hash
|
66
|
+
key_type, value_type = type.entries[0]
|
67
|
+
|
68
|
+
Hash.new(self[key_type],self[value_type])
|
69
|
+
when ::Set
|
70
|
+
Set.new(self[type.entries[0]])
|
71
|
+
when ::Array
|
72
|
+
Array.new(self[type[0]])
|
73
|
+
when ::Proc
|
74
|
+
Proc.new(type)
|
75
|
+
when true
|
76
|
+
Boolean
|
77
|
+
when nil
|
78
|
+
Object
|
79
|
+
when ::Class
|
80
|
+
if type_defined?(type.name)
|
81
|
+
type_get(type.name)
|
82
|
+
else
|
83
|
+
Class.new(type)
|
84
|
+
end
|
85
|
+
when ::Module
|
86
|
+
if type_defined?(type.name)
|
87
|
+
type_get(type.name)
|
88
|
+
else
|
89
|
+
raise(TypeError,"unknown parameter type: #{type.inspect}")
|
90
|
+
end
|
91
|
+
else
|
92
|
+
raise(TypeError,"invalid parameter type: #{type.inspect}")
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'parameters/types/object'
|
2
|
+
|
3
|
+
require 'uri'
|
4
|
+
|
5
|
+
module Parameters
|
6
|
+
module Types
|
7
|
+
class URI < Object
|
8
|
+
|
9
|
+
#
|
10
|
+
# Determines if the value is already a URI.
|
11
|
+
#
|
12
|
+
# @param [Object] value
|
13
|
+
# The value to inspect.
|
14
|
+
#
|
15
|
+
# @return [Boolean]
|
16
|
+
# Specifies whether the value inherits `URI::Generic`.
|
17
|
+
#
|
18
|
+
def self.===(value)
|
19
|
+
value.kind_of?(::URI::Generic)
|
20
|
+
end
|
21
|
+
|
22
|
+
#
|
23
|
+
# Coerces a value into a URI.
|
24
|
+
#
|
25
|
+
# @param [#to_uri, #to_s] value
|
26
|
+
# The value to coerce.
|
27
|
+
#
|
28
|
+
# @return [URI::Generic]
|
29
|
+
# The coerced URI.
|
30
|
+
#
|
31
|
+
def self.coerce(value)
|
32
|
+
if value.respond_to?(:to_uri)
|
33
|
+
value.to_uri
|
34
|
+
else
|
35
|
+
::URI.parse(value.to_s)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|