coercive 1.2.0 → 1.3.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 +4 -4
- data/README.md +24 -0
- data/coercive.gemspec +1 -1
- data/lib/coercive.rb +14 -0
- data/test/coercive.rb +33 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1f107658df43970b3e3cef00a20ed705a11a0f3b9e44ec50c3e391e827c161fb
|
4
|
+
data.tar.gz: df087c93a23d4764f77a816427bbdc7a9c2fc0f75a9137a34a82fc0004bd50c4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e53a1643a5e2f09fc78286cbcf991d535e876d816ca01f59807ada41315e9effd5093206e624f15941eb80d121b291e3c0c5a63e0c0def2b5403aaa1d09daae7
|
7
|
+
data.tar.gz: 5b6668f6bd4241bb7eb1a56a623564be8f63e5538cd9b7cec726e69bf907e11734f4b7f5bc8f4795737847a322ff04b6a3bac0da9ee2ce3c3286b2ec26ef8777
|
data/README.md
CHANGED
@@ -171,6 +171,30 @@ CoerceFoo.call("foo" => 4)
|
|
171
171
|
# => Coercive::Error: {"foo"=>"not_valid"}
|
172
172
|
```
|
173
173
|
|
174
|
+
### `integer`
|
175
|
+
|
176
|
+
`integer` expects an integer value.
|
177
|
+
|
178
|
+
```ruby
|
179
|
+
module CoerceFoo
|
180
|
+
extend Coercive
|
181
|
+
|
182
|
+
attribute :foo, integer, optional
|
183
|
+
end
|
184
|
+
|
185
|
+
CoerceFoo.call("foo" => "1")
|
186
|
+
# => {"foo"=>1}
|
187
|
+
|
188
|
+
CoerceFoo.call("foo" => "bar")
|
189
|
+
# => Coercive::Error: {"foo"=>"not_valid"}
|
190
|
+
|
191
|
+
CoerceFoo.call("foo" => "1.5")
|
192
|
+
# => Coercive::Error: {"foo"=>"not_numeric"}
|
193
|
+
|
194
|
+
CoerceFoo.call("foo" => 1.5)
|
195
|
+
# => Coercive::Error: {"foo"=>"float_not_permitted"}
|
196
|
+
```
|
197
|
+
|
174
198
|
### `float`
|
175
199
|
|
176
200
|
`float` expects, well, a float value.
|
data/coercive.gemspec
CHANGED
data/lib/coercive.rb
CHANGED
@@ -150,6 +150,20 @@ module Coercive
|
|
150
150
|
end
|
151
151
|
end
|
152
152
|
|
153
|
+
# Public DSL: Return a coerce function to coerce input to an Integer.
|
154
|
+
# Used when declaring an attribute. See documentation for attr_coerce_fns.
|
155
|
+
def integer
|
156
|
+
->(input) do
|
157
|
+
fail Coercive::Error.new("float_not_permitted") if input.is_a?(Float)
|
158
|
+
|
159
|
+
begin
|
160
|
+
Integer(input)
|
161
|
+
rescue TypeError, ArgumentError
|
162
|
+
fail Coercive::Error.new("not_numeric")
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
153
167
|
# Public DSL: Return a coerce function to coerce input to a Float.
|
154
168
|
# Used when declaring an attribute. See documentation for attr_coerce_fns.
|
155
169
|
def float
|
data/test/coercive.rb
CHANGED
@@ -100,6 +100,39 @@ describe "Coercive" do
|
|
100
100
|
end
|
101
101
|
end
|
102
102
|
|
103
|
+
describe "integer" do
|
104
|
+
before do
|
105
|
+
@coercion = Module.new do
|
106
|
+
extend Coercive
|
107
|
+
|
108
|
+
attribute :foo, integer, required
|
109
|
+
attribute :bar, integer, optional
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
it "coerces the input value to an integer" do
|
114
|
+
attributes = { "foo" => "100", "bar" => 100 }
|
115
|
+
|
116
|
+
expected = { "foo" => 100, "bar" => 100 }
|
117
|
+
|
118
|
+
assert_equal expected, @coercion.call(attributes)
|
119
|
+
end
|
120
|
+
|
121
|
+
it "doesn't allow Float" do
|
122
|
+
expected_errors = { "foo" => "float_not_permitted" }
|
123
|
+
|
124
|
+
assert_coercion_error(expected_errors) { @coercion.call("foo" => 100.5) }
|
125
|
+
end
|
126
|
+
|
127
|
+
it "errors if the input can't be coerced into an Integer" do
|
128
|
+
["nope", "100.5", "1e5"].each do |value|
|
129
|
+
expected_errors = { "foo" => "not_numeric" }
|
130
|
+
|
131
|
+
assert_coercion_error(expected_errors) { @coercion.call("foo" => value) }
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
103
136
|
describe "float" do
|
104
137
|
before do
|
105
138
|
@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.
|
4
|
+
version: 1.3.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-
|
12
|
+
date: 2020-09-02 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Coercive is a library to validate and coerce user input
|
15
15
|
email:
|