11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
# File 'lib/dynamic_schema/struct.rb', line 11
def new( *arguments, &block )
unless self == ::DynamicSchema::Struct
super
else
__schema = arguments.first
::Kernel.raise ::ArgumentError, "A Struct requires a schema." \
unless __schema
__converter = ::DynamicSchema::Converter
__compiled_schema = __compile_schema( __schema )
__klass = ::Class.new( self ) do
class << self
def build( attributes = {}, &block )
struct = new( attributes )
block.call( struct ) if block
struct
end
def build!( attributes = {}, &block )
struct = build( attributes, &block )
struct.validate!
struct
end
end
def initialize( attributes = nil )
@attributes = attributes&.dup || {}
@converted_attributes = {}
end
__compiled_schema.each do | property, criteria |
key = ( criteria[ :as ] || property ).to_sym
type = criteria[ :type ]
default = criteria[ :default ]
has_multiple_types = type.is_a?( ::Array ) && criteria[ :compiler ]
if has_multiple_types
_criteria = criteria
define_method( property ) do
@converted_attributes.fetch( key ) do
value = @attributes[ key ]
value = default if value.nil?
schema = _criteria[ :schema ] ||= ( _criteria[ :compiler ]&.compiled )
klass = _criteria[ :class ] ||= ( schema ? ::DynamicSchema::Struct.new( schema ) : nil )
@converted_attributes[ key ] =
if _criteria[ :array ]
__convert_types_array( Array( value ), klass, _criteria )
else
__normalize( __convert_types( value, klass, _criteria ), _criteria )
end
end
end
elsif type == ::Object
_criteria = criteria
define_method( property ) do
@converted_attributes.fetch( key ) do
value = @attributes[ key ]
schema = _criteria[ :schema ] ||= ( _criteria[ :compiler ]&.compiled )
return value unless schema
klass = _criteria[ :class ] ||= ::DynamicSchema::Struct.new( schema )
@converted_attributes[ key ] =
if _criteria[ :array ]
Array( value || default ).map { | v | __normalize( klass.build( v || {} ), _criteria ) }
else
__normalize( klass.build( value || default ), _criteria )
end
end
end
elsif type
_criteria = criteria
define_method( property ) do
@converted_attributes.fetch( key ) do
value = @attributes[ key ]
@converted_attributes[ key ] = _criteria[ :array ] ?
Array( value || default ).map { | v | __normalize( __coerce( v, to: type ), _criteria ) } :
__normalize( __coerce( value || default, to: type ), _criteria )
end
end
else
_criteria = criteria
define_method( property ) do
value = @attributes[ key ] || default
if _criteria[ :array ]
Array( value ).map { | v | __normalize( v, _criteria ) }
else
__normalize( value, _criteria )
end
end
end
define_method( :"#{ property }=" ) do | value |
@converted_attributes.delete( key )
@attributes[ key ] = value
end
end
[ :validate!, :validate, :valid? ].each do | method |
define_method( method ) { super( @attributes ) }
end
def to_h
result = {}
self.compiled_schema.each do | property, criteria |
key = criteria[ :as ] || property
value = __object_to_h( self.send( property ) )
result[ key ] = value unless value.nil?
end
result
end
private
def compiled_schema
self.class.compiled_schema
end
def __object_to_h( object )
case object
when nil
nil
when ::Hash
object.transform_values { | v | __object_to_h( v ) } unless object.empty?
when ::Array
object.map { | e | __object_to_h( e ) } unless object.empty?
else
if object.respond_to?( :to_h )
__object_to_h( object.to_h )
else
object
end
end
end
def __coerce( value, to: )
self.class.converter.convert( value, to: to ) { | v | to.new( v ) rescue nil }
end
def __convert_types( value, klass, criteria )
return nil if value.nil?
if value.is_a?( ::Hash )
klass ? klass.build( value ) : value
else
scalar_types = __non_object_types( criteria[ :type ] )
__coerce( value, to: scalar_types )
end
end
def __convert_types_array( values, klass, criteria )
values.map { | v | __normalize( __convert_types( v, klass, criteria ), criteria ) }
end
def __non_object_types( types )
types_array = types.is_a?( ::Array ) ? types : [ types ]
result = types_array.reject { | type | type == ::Object }
result.length == 1 ? result.first : result
end
def __normalize( value, criteria )
normalize = criteria[ :normalize ]
return value unless normalize && !value.nil?
normalize.call( value )
end
end
__klass.instance_variable_set( :@__compiled_schema, __compiled_schema.dup )
__klass.instance_variable_set( :@__converter, __converter )
__klass.class_eval( &block ) if block
__klass
end
end
|