coercive 1.4.0 → 1.5.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
  SHA256:
3
- metadata.gz: f5ece0274ec3187ab9ae2407837048143d6c1c516bd4ddcfc953a208e5bee6ed
4
- data.tar.gz: 6b9ffb6fa9c18a0d9049193c7e352201f559b86a98cd4258d49b756ca8bf4df9
3
+ metadata.gz: a9ce029330ac405fde945a05e6f81c62361027ebe5d03659fd82b067554087e7
4
+ data.tar.gz: 89cfe35313fde4e0b0cea25d6050b008e75c8457ca440b71cf1c2acca26d13d1
5
5
  SHA512:
6
- metadata.gz: d036ba5c9a7b0526903330fe17aff3e34f47f0c0a338769fb0762fed5da8ca7d0210143b0f87533dba2010637feed320cbf454d78d629ffd5d3ebb13a8ab18fb
7
- data.tar.gz: c5eb1b8af3fb355081d15108bf037fd4f5ec9b85be892f875500b419a343a8d4136c26ce0ff3ca0c8fca8bf3bb91f7ac4acf8439369009fbec14dfaf98e5e043
6
+ metadata.gz: d5260061302b501653bf64ce29988622f5ea06739a32058778a64106ad25ddd28567773048ac48addf510adc5f08357f0f6258c2877b67b8e2c0a3e3352baf48
7
+ data.tar.gz: 25e42dec9a930a6cea139f581114abf59d166a95210a5884edb0276a29bbbee7cb365959d51c6caddc4c76c827d0dfecf5bdb71386e262d2e8fccd3b20decacc
data/README.md CHANGED
@@ -202,20 +202,27 @@ CoerceFoo.call("foo_bounds" => 11)
202
202
  # => Coercive::Error: {"foo_bounds"=>"too_high"}
203
203
  ```
204
204
 
205
- ### `float`
205
+ ### `float(min:, max:)`
206
206
 
207
- `float` expects, well, a float value.
207
+ `float` expects, well, a float value. It supports optional `min` and `max` options to check if the user input is within certain bounds.
208
208
 
209
209
  ```ruby
210
210
  module CoerceFoo
211
211
  extend Coercive
212
212
 
213
- attribute :foo, float, optional
213
+ attribute :foo, float, optional
214
+ attribute :foo_bounds, float(min: 1.0, max: 5.5), optional
214
215
  end
215
216
 
216
217
  CoerceFoo.call("foo" => "bar")
217
218
  # => Coercive::Error: {"foo"=>"not_valid"}
218
219
 
220
+ CoerceFoo.call("foo" => "0.5")
221
+ # => Coercive::Error: {"foo"=>"too_low"}
222
+
223
+ CoerceFoo.call("foo" => 6.5)
224
+ # => Coercive::Error: {"foo"=>"too_high"}
225
+
219
226
  CoerceFoo.call("foo" => "0.1")
220
227
  # => {"foo"=>0.1}
221
228
 
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "coercive"
3
- s.version = "1.4.0"
3
+ s.version = "1.5.0"
4
4
  s.summary = "Coercive is a library to validate and coerce user input"
5
5
  s.description = s.summary
6
6
  s.authors = ["Joe McIlvain", "Lucas Tolchinsky"]
@@ -171,13 +171,18 @@ module Coercive
171
171
 
172
172
  # Public DSL: Return a coerce function to coerce input to a Float.
173
173
  # Used when declaring an attribute. See documentation for attr_coerce_fns.
174
- def float
174
+ def float(min: nil, max: nil)
175
175
  ->(input) do
176
- begin
177
- Float(input)
178
- rescue TypeError, ArgumentError
179
- fail Coercive::Error.new("not_numeric")
180
- end
176
+ input = begin
177
+ Float(input)
178
+ rescue TypeError, ArgumentError
179
+ fail Coercive::Error.new("not_numeric")
180
+ end
181
+
182
+ fail Coercive::Error.new("too_low") if min && input < min
183
+ fail Coercive::Error.new("too_high") if max && input > max
184
+
185
+ input
181
186
  end
182
187
  end
183
188
 
@@ -149,7 +149,8 @@ describe "Coercive" do
149
149
  @coercion = Module.new do
150
150
  extend Coercive
151
151
 
152
- attribute :foo, float, required
152
+ attribute :foo, float, optional
153
+ attribute :bar, float(min: 1.0, max: 5.5), optional
153
154
  end
154
155
  end
155
156
 
@@ -176,6 +177,16 @@ describe "Coercive" do
176
177
  assert_coercion_error(expected_errors) { @coercion.call("foo" => bad) }
177
178
  end
178
179
  end
180
+
181
+ it "errors if the input is out of bounds" do
182
+ expected_errors = { "bar" => "too_low" }
183
+
184
+ assert_coercion_error(expected_errors) { @coercion.call("bar" => 0.5) }
185
+
186
+ expected_errors = { "bar" => "too_high" }
187
+
188
+ assert_coercion_error(expected_errors) { @coercion.call("bar" => 6.0) }
189
+ end
179
190
  end
180
191
 
181
192
  describe "string" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: coercive
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joe McIlvain