7
7
require 'shallow_attributes/type/time'
8
8
require 'shallow_attributes/type/date'
9
9
10
+ # Boolean class for working with bool values
11
+ #
12
+ # @private
13
+ #
14
+ # @since 0.9.4
15
+ class Boolean ; end
16
+
10
17
module ShallowAttributes
11
18
# Namespace for standard type classes
12
19
#
@@ -30,7 +37,8 @@ class InvalidValueError < TypeError
30
37
::Integer => ShallowAttributes ::Type ::Integer . new ,
31
38
::String => ShallowAttributes ::Type ::String . new ,
32
39
::Time => ShallowAttributes ::Type ::Time . new ,
33
- ::Date => ShallowAttributes ::Type ::Date . new
40
+ ::Date => ShallowAttributes ::Type ::Date . new ,
41
+ ::Boolean => ShallowAttributes ::Type ::Boolean . new
34
42
} . freeze
35
43
36
44
class << self
@@ -56,7 +64,10 @@ class << self
56
64
#
57
65
# @since 0.1.0
58
66
def coerce ( type , value , options = { } )
59
- type_instance ( type ) . coerce ( value , options )
67
+ instance = type_instance ( type , value )
68
+ return instance . coerce ( instance . attributes , options ) if instance . respond_to? :attributes
69
+
70
+ instance . coerce ( value , options )
60
71
end
61
72
62
73
private
@@ -78,8 +89,14 @@ def coerce(type, value, options = {})
78
89
# @return [Class]
79
90
#
80
91
# @since 0.1.0
81
- def type_instance ( klass )
82
- DEFAULT_TYPE_OBJECTS [ klass ] || ShallowAttributes ::Type . const_get ( klass . name ) . new
92
+ def type_instance ( klass , value = { } )
93
+ return DEFAULT_TYPE_OBJECTS [ klass ] if DEFAULT_TYPE_OBJECTS [ klass ]
94
+
95
+ instantiable = ShallowAttributes ::Type . const_get ( klass . name )
96
+
97
+ return instantiable . new ( value ) if instantiable < ShallowAttributes
98
+
99
+ instantiable . new
83
100
end
84
101
end
85
102
end
0 commit comments