coercive 1.5.0 → 1.6.0

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
  SHA256:
3
- metadata.gz: a9ce029330ac405fde945a05e6f81c62361027ebe5d03659fd82b067554087e7
4
- data.tar.gz: 89cfe35313fde4e0b0cea25d6050b008e75c8457ca440b71cf1c2acca26d13d1
3
+ metadata.gz: af1258f102aed9654f229199a6999f7dc817550b51fd009d9e8057de26c8cb11
4
+ data.tar.gz: 297d85ff20739bf100f7434d657a842adbaa783a17b10c459325724391d84196
5
5
  SHA512:
6
- metadata.gz: d5260061302b501653bf64ce29988622f5ea06739a32058778a64106ad25ddd28567773048ac48addf510adc5f08357f0f6258c2877b67b8e2c0a3e3352baf48
7
- data.tar.gz: 25e42dec9a930a6cea139f581114abf59d166a95210a5884edb0276a29bbbee7cb365959d51c6caddc4c76c827d0dfecf5bdb71386e262d2e8fccd3b20decacc
6
+ metadata.gz: e9e1e6a9d98a32bc3819ef6ea4765f21a45ed103000186d71d394d10ac657178eb6498d55bebd2b7a54182cb6565593efc8f377f1ba8bf4074dbed030a81704f
7
+ data.tar.gz: 9cde425ae7847267b73118f741304a2a1b1b04cd7585d52c5d1bb0a5f51b664dd530c00a5e50c3bdecf9f0c0e3e3c1cc4a8a567a472e0d6745d28e26f84b76cb
data/README.md CHANGED
@@ -217,19 +217,44 @@ end
217
217
  CoerceFoo.call("foo" => "bar")
218
218
  # => Coercive::Error: {"foo"=>"not_valid"}
219
219
 
220
- CoerceFoo.call("foo" => "0.5")
221
- # => Coercive::Error: {"foo"=>"too_low"}
220
+ CoerceFoo.call("foo_bounds" => "0.5")
221
+ # => Coercive::Error: {"foo_bounds"=>"too_low"}
222
222
 
223
- CoerceFoo.call("foo" => 6.5)
224
- # => Coercive::Error: {"foo"=>"too_high"}
223
+ CoerceFoo.call("foo_bounds" => 6.5)
224
+ # => Coercive::Error: {"foo_bounds"=>"too_high"}
225
225
 
226
226
  CoerceFoo.call("foo" => "0.1")
227
227
  # => {"foo"=>0.1}
228
-
228
+
229
229
  CoerceFoo.call("foo" => "0.1e5")
230
230
  # => {"foo"=>10000.0}
231
231
  ```
232
232
 
233
+ ### `boolean(true_if:, false_if:)
234
+
235
+ `boolean` will coerce input into `true` or `false`. You can also specifiy additional values to coerce into `true` or `false` with the `true_if` and `false_if` options.
236
+
237
+ ```ruby
238
+ module CoerceFoo
239
+ extend Coercive
240
+
241
+ attribute :foo, boolean, optional
242
+ attribute :foo_if, boolean(true_if: member(["1", "on"])), optional
243
+ end
244
+
245
+ CoerceFoo.call("foo" => true)
246
+ # => {"foo"=>true}
247
+
248
+ CoerceFoo.call("foo" => "true")
249
+ # => {"foo"=>true}
250
+
251
+ CoerceFoo.call("foo" => nil)
252
+ # => Coercive::Error: {"foo"=>"not_valid"}
253
+
254
+ CoerceFoo.call("foo_if" => "on")
255
+ # => {"foo_if"=>true}
256
+ ```
257
+
233
258
  ### `array`
234
259
 
235
260
  The `array` coercion is interesting because it's where `Coercive` starts to shine, by letting you compose coercion functions together. Let's see:
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "coercive"
3
- s.version = "1.5.0"
3
+ s.version = "1.6.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"]
@@ -186,6 +186,33 @@ module Coercive
186
186
  end
187
187
  end
188
188
 
189
+ # Public DSL: Return a coerce function to coerce input to true or false.
190
+ # Used when declaring an attribute. See documentation for attr_coerce_fns.
191
+ #
192
+ # true_if - coerce function to override which values will coerce to true.
193
+ # false_if - coerce function to override which values will coerce to false.
194
+ def boolean(true_if: member([true, "true"]), false_if: member([false, "false"]))
195
+ ->(input) do
196
+ test_success = ->(coerce_fn) do
197
+ begin
198
+ coerce_fn.call(input)
199
+ rescue Coercive::Error
200
+ return false
201
+ end
202
+
203
+ true
204
+ end
205
+
206
+ if test_success.(true_if)
207
+ true
208
+ elsif test_success.(false_if)
209
+ false
210
+ else
211
+ fail Coercive::Error.new("not_valid")
212
+ end
213
+ end
214
+ end
215
+
189
216
  # Public DSL: Return a coerce function to coerce input to a String.
190
217
  # Used when declaring an attribute. See documentation for attr_coerce_fns.
191
218
  #
@@ -137,7 +137,7 @@ describe "Coercive" do
137
137
  expected_errors = { "baz" => "too_low" }
138
138
 
139
139
  assert_coercion_error(expected_errors) { @coercion.call("baz" => 0) }
140
-
140
+
141
141
  expected_errors = { "baz" => "too_high" }
142
142
 
143
143
  assert_coercion_error(expected_errors) { @coercion.call("baz" => 11) }
@@ -182,13 +182,71 @@ describe "Coercive" do
182
182
  expected_errors = { "bar" => "too_low" }
183
183
 
184
184
  assert_coercion_error(expected_errors) { @coercion.call("bar" => 0.5) }
185
-
185
+
186
186
  expected_errors = { "bar" => "too_high" }
187
187
 
188
188
  assert_coercion_error(expected_errors) { @coercion.call("bar" => 6.0) }
189
189
  end
190
190
  end
191
191
 
192
+ describe "boolean" do
193
+ before do
194
+ @coercion = Module.new do
195
+ extend Coercive
196
+
197
+ attribute :foo, boolean, optional
198
+ attribute :foo_true, boolean(true_if: member(["on", 1])), optional
199
+ attribute :foo_false, boolean(false_if: member(["off", "0"])), optional
200
+ end
201
+ end
202
+
203
+ it "supports true or false as String by default" do
204
+ [true, "true"].each do |value|
205
+ attributes = { "foo" => value }
206
+
207
+ expected = { "foo" => true }
208
+
209
+ assert_equal expected, @coercion.call(attributes)
210
+ end
211
+
212
+ [false, "false"].each do |value|
213
+ attributes = { "foo" => value }
214
+
215
+ expected = { "foo" => false }
216
+
217
+ assert_equal expected, @coercion.call(attributes)
218
+ end
219
+ end
220
+
221
+ it "supports customizing values to coerce into true or false" do
222
+ ["on", 1].each do |value|
223
+ attributes = { "foo_true" => value }
224
+
225
+ expected = { "foo_true" => true }
226
+
227
+ assert_equal expected, @coercion.call(attributes)
228
+ end
229
+
230
+ ["off", "0"].each do |value|
231
+ attributes = { "foo_false" => value }
232
+
233
+ expected = { "foo_false" => false }
234
+
235
+ assert_equal expected, @coercion.call(attributes)
236
+ end
237
+ end
238
+
239
+ it "fails on unknown values" do
240
+ ["nope", nil].each do |value|
241
+ attributes = { "foo" => value }
242
+
243
+ expected_errors = { "foo" => "not_valid" }
244
+
245
+ assert_coercion_error(expected_errors) { @coercion.call(attributes) }
246
+ end
247
+ end
248
+ end
249
+
192
250
  describe "string" do
193
251
  before do
194
252
  @coercion = Module.new 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.5.0
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joe McIlvain
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-09-03 00:00:00.000000000 Z
12
+ date: 2020-09-21 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Coercive is a library to validate and coerce user input
15
15
  email: