rschema 3.0.1.pre1 → 3.0.1.pre2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rschema/http_coercer.rb +39 -15
- data/lib/rschema/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f19805cfd6ac29a7633c31b7ac0cc66fdf90b3e1
|
4
|
+
data.tar.gz: 1d9f3aa2548b2f2328fa4d061885f944261c32e1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3c210cea14f881ab5777fe8d8da15c05f4034f9e807b291db4e59c64fc345fa56f18cbf72df0eb7121971ce07203f34d18ed1ac4b98c4e5426be58bfa9167652
|
7
|
+
data.tar.gz: 8f6c6df121f4a89813d74187008fa8a563b494412998138c351e1ae4582b8f3d41aa8a47de8d53cbd2c670d6b5b25fb612e3128d84c1a0533fc9154b1a671de9
|
data/lib/rschema/http_coercer.rb
CHANGED
@@ -33,11 +33,16 @@ module RSchema
|
|
33
33
|
end
|
34
34
|
|
35
35
|
def with_wrapped_subschemas(wrapper)
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
36
|
+
# Double wrapping is potentially a problem. Coercers expect their
|
37
|
+
# subschemas to be a particular type. If their subschema gets wrapped
|
38
|
+
# again, the type changes, so if the coercer tries to use its subschema
|
39
|
+
# during coercion, it will crash.
|
40
|
+
#
|
41
|
+
# For this reason, coercers must not rely upon the type of their
|
42
|
+
# subschemas within `#call`. Coercer schemas should store any required
|
43
|
+
# info from their subschemas within `#initialize`.
|
44
|
+
|
45
|
+
self.class.new(wrapper.wrap(subschema))
|
41
46
|
end
|
42
47
|
|
43
48
|
def invalid!
|
@@ -113,6 +118,14 @@ module RSchema
|
|
113
118
|
end
|
114
119
|
|
115
120
|
class FixedHashCoercer < Coercer
|
121
|
+
attr_reader :hash_attributes
|
122
|
+
|
123
|
+
def initialize(fixed_hash_schema, attributes = nil)
|
124
|
+
super(fixed_hash_schema)
|
125
|
+
|
126
|
+
@hash_attributes = attributes || fixed_hash_schema.attributes.map(&:dup)
|
127
|
+
end
|
128
|
+
|
116
129
|
def coerce(value)
|
117
130
|
default_bools_to_false(symbolize_keys(value))
|
118
131
|
end
|
@@ -129,18 +142,18 @@ module RSchema
|
|
129
142
|
end
|
130
143
|
|
131
144
|
def keys_to_symbolize(hash)
|
132
|
-
#
|
133
|
-
symbol_keys =
|
145
|
+
# some of this could be memoized
|
146
|
+
symbol_keys = hash_attributes
|
134
147
|
.map(&:key)
|
135
148
|
.select{ |k| k.is_a?(Symbol) }
|
136
149
|
.map(&:to_s)
|
137
150
|
|
138
|
-
string_keys =
|
151
|
+
string_keys = hash_attributes
|
139
152
|
.map(&:key)
|
140
153
|
.select{ |k| k.is_a?(String) }
|
141
154
|
|
142
155
|
hash.keys.select do |k|
|
143
|
-
|
156
|
+
symbol_keys.include?(k) && !string_keys.include?(k)
|
144
157
|
end
|
145
158
|
end
|
146
159
|
|
@@ -151,19 +164,30 @@ module RSchema
|
|
151
164
|
#
|
152
165
|
# This method coerces these missing values into `false`.
|
153
166
|
|
154
|
-
|
155
|
-
keys_to_default = subschema.attributes
|
156
|
-
.select { |attr| attr.value_schema.is_a?(BoolCoercer) }
|
157
|
-
.map(&:key)
|
167
|
+
missing_keys = keys_for_bool_defaulting
|
158
168
|
.reject { |key| hash.has_key?(key) }
|
159
169
|
|
160
|
-
if
|
161
|
-
defaults =
|
170
|
+
if missing_keys.any?
|
171
|
+
defaults = missing_keys.map{ |k| [k, false] }.to_h
|
162
172
|
hash.merge(defaults)
|
163
173
|
else
|
164
174
|
hash # no coercion necessary
|
165
175
|
end
|
166
176
|
end
|
177
|
+
|
178
|
+
def with_wrapped_subschemas(wrapper)
|
179
|
+
self.class.new(wrapper.wrap(subschema), hash_attributes)
|
180
|
+
end
|
181
|
+
|
182
|
+
private
|
183
|
+
|
184
|
+
def keys_for_bool_defaulting
|
185
|
+
# this could be memoized
|
186
|
+
hash_attributes
|
187
|
+
.reject(&:optional)
|
188
|
+
.select { |attr| attr.value_schema.is_a?(BoolCoercer) }
|
189
|
+
.map(&:key)
|
190
|
+
end
|
167
191
|
end
|
168
192
|
|
169
193
|
TYPE_COERCERS = {
|
data/lib/rschema/version.rb
CHANGED