coercive 1.2.0 → 1.3.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: 7ffc4474a94a2f1e246925e54549fdbf360fdc272ebdb670a99152b7ec6418b9
4
- data.tar.gz: 92bdc78e8286dce0ce88464b843777f3c7b60bbb864a4c3daca3e3b80a9d6c8c
3
+ metadata.gz: 1f107658df43970b3e3cef00a20ed705a11a0f3b9e44ec50c3e391e827c161fb
4
+ data.tar.gz: df087c93a23d4764f77a816427bbdc7a9c2fc0f75a9137a34a82fc0004bd50c4
5
5
  SHA512:
6
- metadata.gz: 7a41ea8301735ce0b45d4c6d2e96a9e8c0bb06399c8b4f47be1f115d8996c640714a36e7211d4abc73e5ca40944f9be387b31eed0e9c8b22f7e859f10bd59480
7
- data.tar.gz: e9f73c7c476d50e49bfbc7433d3b9da14541999a92582462e5ca1042ef80743a748ce83af2eb418ffb54ec393d4196cba1a49f5f5008aa4c20907918af915e7d
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.
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "coercive"
3
- s.version = "1.2.0"
3
+ s.version = "1.3.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"]
@@ -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
@@ -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.2.0
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-08-31 00:00:00.000000000 Z
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: