rschema 3.1.0 → 3.1.1

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: 0004e8f7543e3268f043077e5de29acb277b54c2
4
- data.tar.gz: b63f12965664d76c537e8abfff9ec51b83eac396
3
+ metadata.gz: d720955c9a91f41ca082da8683dcf7bed5d10ab6
4
+ data.tar.gz: 6d374b1df2484fa2ec0957ca3d5943289ecb9714
5
5
  SHA512:
6
- metadata.gz: 9796286f10fa653137a59fda54d062a0812cc7d87c1fc4caa77aceedc70ef72470a553e03e7756ae8712eef3dafe98c394d7c059fa53d4958af3b6e8be37f569
7
- data.tar.gz: 027dd033a77d5200dfe5069c7e5b3f4acc7d3019c079d76c0724f38d4b82542544cd81a27ad68eebfcbd3c3314998acc28a53e8c9862b571b47f5c4ce5f0aa1f
6
+ metadata.gz: 2152a4189beed6ab21c9fd11afbf668b32cbca48eb58c89820b424473c83ac766e2819f49dc10e456690c4e75a7c2dcb3be1f52cfdc4e89528f34c01c0c45209
7
+ data.tar.gz: d4ccfbf1529449d6f13633898dde3c5c65f363ba041724503214e68f23720a02c2870c0b0215ed5fd3324e71ff6f49a590ac4eb2bd8c6e4973e9fc2b0ca732cf
@@ -20,6 +20,10 @@ module Coercers
20
20
  Result.failure
21
21
  end
22
22
 
23
+ def will_affect?(value)
24
+ subcoercers.any?{ |sc| sc.will_affect?(value) }
25
+ end
26
+
23
27
  class Builder
24
28
  attr_reader :subbuilders
25
29
 
@@ -24,6 +24,10 @@ module Coercers
24
24
  else Result.failure
25
25
  end
26
26
  end
27
+
28
+ def will_affect?(value)
29
+ true != value && false != value
30
+ end
27
31
  end
28
32
 
29
33
  end
@@ -21,6 +21,10 @@ module Coercers
21
21
  result
22
22
  end
23
23
 
24
+ def will_affect?(value)
25
+ subcoercers.any? { |sc| sc.will_affect?(value) }
26
+ end
27
+
24
28
  class Builder
25
29
  attr_reader :subbuilders
26
30
 
@@ -19,6 +19,10 @@ module Coercers
19
19
  Result.failure
20
20
  end
21
21
  end
22
+
23
+ def will_affect?(value)
24
+ not value.is_a?(::Date)
25
+ end
22
26
  end
23
27
 
24
28
  end
@@ -26,9 +26,13 @@ module FixedHash
26
26
  Result.success(default_arrays_to_empty(value))
27
27
  end
28
28
 
29
+ def will_affect?(value)
30
+ keys_to_default(value).any?
31
+ end
32
+
29
33
  private
30
34
  def default_arrays_to_empty(hash)
31
- missing_keys = keys_for_array_defaulting - hash.keys
35
+ missing_keys = keys_to_default(hash)
32
36
 
33
37
  if missing_keys.any?
34
38
  defaults = missing_keys.map{ |k| [k, []] }.to_h
@@ -38,6 +42,14 @@ module FixedHash
38
42
  end
39
43
  end
40
44
 
45
+ def keys_to_default(value)
46
+ if value.is_a?(Hash)
47
+ keys_for_array_defaulting - value.keys
48
+ else
49
+ []
50
+ end
51
+ end
52
+
41
53
  def keys_for_array_defaulting
42
54
  @keys_for_array_defaulting ||= Set.new(
43
55
  hash_attributes
@@ -25,9 +25,13 @@ module FixedHash
25
25
  Result.success(default_bools_to_false(value))
26
26
  end
27
27
 
28
+ def will_affect?(value)
29
+ keys_to_default(value).any?
30
+ end
31
+
28
32
  private
29
33
  def default_bools_to_false(hash)
30
- missing_keys = keys_for_bool_defaulting - hash.keys
34
+ missing_keys = keys_to_default(hash)
31
35
 
32
36
  if missing_keys.any?
33
37
  defaults = missing_keys.map{ |k| [k, false] }.to_h
@@ -37,6 +41,14 @@ module FixedHash
37
41
  end
38
42
  end
39
43
 
44
+ def keys_to_default(value)
45
+ if value.is_a?(Hash)
46
+ keys_for_bool_defaulting - value.keys
47
+ else
48
+ []
49
+ end
50
+ end
51
+
40
52
  def keys_for_bool_defaulting
41
53
  @keys_for_bool_defaulting ||= Set.new(
42
54
  hash_attributes
@@ -18,20 +18,32 @@ module FixedHash
18
18
  Result.success(remove_extraneous_elements(value))
19
19
  end
20
20
 
21
+ def will_affect?(value)
22
+ keys_to_remove(value).any?
23
+ end
24
+
21
25
  private
22
26
 
23
27
  def remove_extraneous_elements(hash)
24
- keys_to_remove = hash.keys - valid_keys
28
+ extra_keys = keys_to_remove(hash)
25
29
 
26
- if keys_to_remove.any?
30
+ if extra_keys.any?
27
31
  hash.dup.tap do |stripped_hash|
28
- keys_to_remove.each { |k| stripped_hash.delete(k) }
32
+ extra_keys.each { |k| stripped_hash.delete(k) }
29
33
  end
30
34
  else
31
35
  hash
32
36
  end
33
37
  end
34
38
 
39
+ def keys_to_remove(value)
40
+ if value.is_a?(Hash)
41
+ value.keys - valid_keys
42
+ else
43
+ []
44
+ end
45
+ end
46
+
35
47
  def valid_keys
36
48
  @valid_keys ||= hash_attributes.map(&:key)
37
49
  end
@@ -20,6 +20,10 @@ module FixedHash
20
20
  Result.success(symbolize_keys(value))
21
21
  end
22
22
 
23
+ def will_affect?(value)
24
+ keys_to_symbolize(hash).any?
25
+ end
26
+
23
27
  private
24
28
 
25
29
  def symbolize_keys(hash)
@@ -33,9 +37,13 @@ module FixedHash
33
37
  end
34
38
  end
35
39
 
36
- def keys_to_symbolize(hash)
37
- non_string_keys = Set.new(hash.keys) - string_keys
38
- non_string_keys.intersection(symbol_keys_as_strings)
40
+ def keys_to_symbolize(value)
41
+ if value.is_a?(Hash)
42
+ non_string_keys = Set.new(value.keys) - string_keys
43
+ non_string_keys.intersection(symbol_keys_as_strings)
44
+ else
45
+ []
46
+ end
39
47
  end
40
48
 
41
49
  def symbol_keys_as_strings
@@ -12,6 +12,10 @@ module Coercers
12
12
  flt = Float(value) rescue nil
13
13
  flt ? Result.success(flt) : Result.failure
14
14
  end
15
+
16
+ def will_affect?(value)
17
+ not value.is_a?(Float)
18
+ end
15
19
  end
16
20
 
17
21
  end
@@ -12,6 +12,10 @@ module Coercers
12
12
  int = Integer(value) rescue nil
13
13
  int ? Result.success(int) : Result.failure
14
14
  end
15
+
16
+ def will_affect?(value)
17
+ not value.is_a?(Integer)
18
+ end
15
19
  end
16
20
 
17
21
  end
@@ -9,12 +9,16 @@ module Coercers
9
9
  end
10
10
 
11
11
  def call(value)
12
- if value.is_a?(String) && value.empty?
12
+ if "" == value
13
13
  Result.success(nil)
14
14
  else
15
15
  Result.success(value)
16
16
  end
17
17
  end
18
+
19
+ def will_affect?(value)
20
+ "" == value
21
+ end
18
22
  end
19
23
 
20
24
  end
@@ -15,6 +15,10 @@ module Coercers
15
15
  else Result.failure
16
16
  end
17
17
  end
18
+
19
+ def will_affect?(value)
20
+ not value.is_a?(Symbol)
21
+ end
18
22
  end
19
23
 
20
24
  end
@@ -19,6 +19,10 @@ module Coercers
19
19
  Result.failure
20
20
  end
21
21
  end
22
+
23
+ def will_affect?(value)
24
+ not value.is_a?(Time)
25
+ end
22
26
  end
23
27
 
24
28
  end
@@ -1,3 +1,5 @@
1
+ require 'date'
2
+
1
3
  module RSchema
2
4
  class CoercionWrapper
3
5
 
@@ -18,6 +18,11 @@ class Coercer
18
18
  end
19
19
 
20
20
  def call(value, options)
21
+ unless coercer.will_affect?(value)
22
+ # short-circuit the coercer
23
+ return @subschema.call(value, options)
24
+ end
25
+
21
26
  result = coercer.call(value)
22
27
  if result.valid?
23
28
  @subschema.call(result.value, options)
@@ -1,3 +1,3 @@
1
1
  module RSchema
2
- VERSION = '3.1.0'
2
+ VERSION = '3.1.1'
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.1.0
4
+ version: 3.1.1
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-30 00:00:00.000000000 Z
11
+ date: 2017-06-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: docile
@@ -108,6 +108,48 @@ dependencies:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: benchmark-ips
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: activemodel
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: dry-validation
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
111
153
  description: |2
112
154
  Schema-based validation and coercion for Ruby data structures, inspired
113
155
  by Prismatic/schema for Clojure.