rschema 3.0.3 → 3.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7bd4da520ac3e5fb967d27652e353cd91bf9d3f6
4
- data.tar.gz: d36b77ea5fd9224c85850be67c8af4eb9d1a9f11
3
+ metadata.gz: 0004e8f7543e3268f043077e5de29acb277b54c2
4
+ data.tar.gz: b63f12965664d76c537e8abfff9ec51b83eac396
5
5
  SHA512:
6
- metadata.gz: 18a5ca5072280a91495b6c12040097c7eed60e9722c5ef7395634a16c932568b428d20fff49f95672e3fd063cf3ef4fa4e7c6262909213eb20607940624488af
7
- data.tar.gz: 43353ceb29e8959d554eac95e8ed8519af08ccceba308a4b8a15786f8cc5edbbd8d2af205e73ca1397a3235950a0a51380328a786a3e5408c0e57bf48285d95c
6
+ metadata.gz: 9796286f10fa653137a59fda54d062a0812cc7d87c1fc4caa77aceedc70ef72470a553e03e7756ae8712eef3dafe98c394d7c059fa53d4958af3b6e8be37f569
7
+ data.tar.gz: 027dd033a77d5200dfe5069c7e5b3f4acc7d3019c079d76c0724f38d4b82542544cd81a27ad68eebfcbd3c3314998acc28a53e8c9862b571b47f5c4ce5f0aa1f
@@ -0,0 +1,63 @@
1
+ require 'set'
2
+
3
+ module RSchema
4
+ module Coercers
5
+ module FixedHash
6
+
7
+ # The HTTP standard says that when a form is submitted, all unchecked
8
+ # check boxes will _not_ be sent to the server. That is, they will not
9
+ # be present at all in the params hash.
10
+ #
11
+ # This class coerces these missing values into an empty array, where an array
12
+ # is expected.
13
+ class DefaultArraysToEmpty
14
+ attr_reader :hash_attributes
15
+
16
+ def self.build(schema)
17
+ new(schema)
18
+ end
19
+
20
+ def initialize(fixed_hash_schema)
21
+ #TODO: make fixed hash attributes frozen, and eliminate dup
22
+ @hash_attributes = fixed_hash_schema.attributes.map(&:dup)
23
+ end
24
+
25
+ def call(value)
26
+ Result.success(default_arrays_to_empty(value))
27
+ end
28
+
29
+ private
30
+ def default_arrays_to_empty(hash)
31
+ missing_keys = keys_for_array_defaulting - hash.keys
32
+
33
+ if missing_keys.any?
34
+ defaults = missing_keys.map{ |k| [k, []] }.to_h
35
+ hash.merge(defaults)
36
+ else
37
+ hash # no coercion necessary
38
+ end
39
+ end
40
+
41
+ def keys_for_array_defaulting
42
+ @keys_for_array_defaulting ||= Set.new(
43
+ hash_attributes
44
+ .reject(&:optional)
45
+ .select { |attr| is_array_schema?(attr.value_schema) }
46
+ .map(&:key)
47
+ )
48
+ end
49
+
50
+ def is_array_schema?(schema)
51
+ # dig through all the coercers
52
+ non_coercer = schema
53
+ while non_coercer.is_a?(Schemas::Coercer)
54
+ non_coercer = non_coercer.subschema
55
+ end
56
+
57
+ non_coercer.is_a?(Schemas::VariableLengthArray)
58
+ end
59
+ end
60
+
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,21 @@
1
+ module RSchema
2
+ module Coercers
3
+
4
+ module NilEmptyStrings
5
+ extend self
6
+
7
+ def build(schema)
8
+ self
9
+ end
10
+
11
+ def call(value)
12
+ if value.is_a?(String) && value.empty?
13
+ Result.success(nil)
14
+ else
15
+ Result.success(value)
16
+ end
17
+ end
18
+ end
19
+
20
+ end
21
+ end
@@ -8,11 +8,13 @@ class CoercionWrapper
8
8
  coerce_type Time, with: Coercers::Time
9
9
  coerce_type Date, with: Coercers::Date
10
10
 
11
+ coerce Schemas::Maybe, with: Coercers::NilEmptyStrings
11
12
  coerce Schemas::Boolean, with: Coercers::Boolean
12
13
  coerce Schemas::FixedHash, with: Coercers::Chain[
13
14
  Coercers::FixedHash::SymbolizeKeys,
14
15
  Coercers::FixedHash::RemoveExtraneousAttributes,
15
16
  Coercers::FixedHash::DefaultBooleansToFalse,
17
+ Coercers::FixedHash::DefaultArraysToEmpty,
16
18
  ]
17
19
  end
18
20
 
@@ -1,3 +1,3 @@
1
1
  module RSchema
2
- VERSION = '3.0.3'
2
+ VERSION = '3.1.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rschema
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.3
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Dalling
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-28 00:00:00.000000000 Z
11
+ date: 2017-05-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: docile
@@ -125,11 +125,13 @@ files:
125
125
  - lib/rschema/coercers/boolean.rb
126
126
  - lib/rschema/coercers/chain.rb
127
127
  - lib/rschema/coercers/date.rb
128
+ - lib/rschema/coercers/fixed_hash/default_arrays_to_empty.rb
128
129
  - lib/rschema/coercers/fixed_hash/default_booleans_to_false.rb
129
130
  - lib/rschema/coercers/fixed_hash/remove_extraneous_attributes.rb
130
131
  - lib/rschema/coercers/fixed_hash/symbolize_keys.rb
131
132
  - lib/rschema/coercers/float.rb
132
133
  - lib/rschema/coercers/integer.rb
134
+ - lib/rschema/coercers/nil_empty_strings.rb
133
135
  - lib/rschema/coercers/symbol.rb
134
136
  - lib/rschema/coercers/time.rb
135
137
  - lib/rschema/coercion_wrapper.rb