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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4bf71f16e83556e8bd0ac267c50c3e0bb2b02210
4
- data.tar.gz: 44e531a24e345526b22def8a8df44c72f111b636
3
+ metadata.gz: f19805cfd6ac29a7633c31b7ac0cc66fdf90b3e1
4
+ data.tar.gz: 1d9f3aa2548b2f2328fa4d061885f944261c32e1
5
5
  SHA512:
6
- metadata.gz: 414b8192259952378b14ce845e6c333306d18cbee48dc12592d91d3dd7c89fcf6481af96e65d1bcb26d5c07982ea5d1393011b8aaa589f4d76a68bfc1ea444d6
7
- data.tar.gz: 6b3f20c7f6d1d89110c4245788a4a42d1556edfaaee3497dfa0c2aee1eb86fe1a96f0b35ddeea628ae9294391f9e2082cd1ffe410b628e470eb3bae9382447f9
6
+ metadata.gz: 3c210cea14f881ab5777fe8d8da15c05f4034f9e807b291db4e59c64fc345fa56f18cbf72df0eb7121971ce07203f34d18ed1ac4b98c4e5426be58bfa9167652
7
+ data.tar.gz: 8f6c6df121f4a89813d74187008fa8a563b494412998138c351e1ae4582b8f3d41aa8a47de8d53cbd2c670d6b5b25fb612e3128d84c1a0533fc9154b1a671de9
@@ -33,11 +33,16 @@ module RSchema
33
33
  end
34
34
 
35
35
  def with_wrapped_subschemas(wrapper)
36
- raise CanNotBeWrappedError, <<~EOS
37
- This schema has already been wrapped by RSchema::HTTPCoercer.
38
- Wrapping the schema again will most likely result in a schema that
39
- crashes when it is called.
40
- EOS
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
- # these could be cached if we know for sure that the subschema is immutable
133
- symbol_keys = subschema.attributes
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 = subschema.attributes
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
- k.is_a?(String) && symbol_keys.include?(k) && !string_keys.include?(k)
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
- # some of this could be cached if we know for sure that the subschema is immutable
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 keys_to_default.any?
161
- defaults = keys_to_default.map{ |k| [k, false] }.to_h
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 = {
@@ -1,3 +1,3 @@
1
1
  module RSchema
2
- VERSION = '3.0.1.pre1'
2
+ VERSION = '3.0.1.pre2'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rschema
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.1.pre1
4
+ version: 3.0.1.pre2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Dalling