coercive 1.4.0 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -3
- data/coercive.gemspec +1 -1
- data/lib/coercive.rb +11 -6
- data/test/coercive.rb +12 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a9ce029330ac405fde945a05e6f81c62361027ebe5d03659fd82b067554087e7
|
4
|
+
data.tar.gz: 89cfe35313fde4e0b0cea25d6050b008e75c8457ca440b71cf1c2acca26d13d1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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,
|
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
|
|
data/coercive.gemspec
CHANGED
data/lib/coercive.rb
CHANGED
@@ -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
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
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
|
|
data/test/coercive.rb
CHANGED
@@ -149,7 +149,8 @@ describe "Coercive" do
|
|
149
149
|
@coercion = Module.new do
|
150
150
|
extend Coercive
|
151
151
|
|
152
|
-
attribute :foo, float,
|
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
|