hash_cast 0.5.3 → 0.5.4
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/CHANGELOG.md +3 -0
- data/Gemfile.lock +1 -1
- data/lib/hash_cast/casters/integer_caster.rb +29 -10
- data/lib/hash_cast/version.rb +1 -1
- data/spec/hash_cast/casters/integer_caster_spec.rb +49 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b1ff1d676f80eaba8132c7027e6b70f59047a31df70a19f55183ecaff1d915cf
|
4
|
+
data.tar.gz: c1d44f71ee442b52abd55703e96dd4eca331e2ef63329e7fc17188d9bc053200
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9e7a988fa4dffefccb42d33249c923fb475b3f1e4605aa959d20ab1e92e44dc6ada092df1afd720eb4f5d5d0484b6182fb3b90822f00d50363f36a38e55b622e
|
7
|
+
data.tar.gz: df5c9a15f7c1ba2c25a7830d1426fb218202f16ce02cecab6e1234e0b8839e8ae31bd8150c15c6a58733b99e7b5d79bc61787d7133f7b12ab9360d7b36fb3a5e
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,17 +1,36 @@
|
|
1
1
|
class HashCast::Casters::IntegerCaster
|
2
2
|
|
3
|
+
DEFAULT_MIN_VALUE = 2**31 * -1
|
4
|
+
DEFAULT_MAX_VALUE = 2**31
|
5
|
+
|
3
6
|
def self.cast(value, attr_name, options = {})
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
Integer(value)
|
9
|
-
rescue ArgumentError => e
|
10
|
-
raise HashCast::Errors::CastingError, "is invalid integer"
|
11
|
-
end
|
12
|
-
else
|
13
|
-
raise HashCast::Errors::CastingError, "should be a integer"
|
7
|
+
integer_value = get_integer_value(value)
|
8
|
+
|
9
|
+
if integer_value < options.fetch(:min_value, DEFAULT_MIN_VALUE)
|
10
|
+
raise HashCast::Errors::CastingError, "should be within allowed range"
|
14
11
|
end
|
12
|
+
|
13
|
+
if integer_value > options.fetch(:max_value, DEFAULT_MAX_VALUE)
|
14
|
+
raise HashCast::Errors::CastingError, "should be within allowed range"
|
15
|
+
end
|
16
|
+
|
17
|
+
integer_value
|
15
18
|
end
|
16
19
|
|
20
|
+
private
|
21
|
+
def self.get_integer_value(value)
|
22
|
+
if value.is_a?(Integer)
|
23
|
+
return value
|
24
|
+
end
|
25
|
+
|
26
|
+
if value.is_a?(String)
|
27
|
+
begin
|
28
|
+
return Integer(value)
|
29
|
+
rescue ArgumentError => e
|
30
|
+
raise HashCast::Errors::CastingError, "is invalid integer"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
raise HashCast::Errors::CastingError, "should be a integer"
|
35
|
+
end
|
17
36
|
end
|
data/lib/hash_cast/version.rb
CHANGED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe HashCast::Casters::IntegerCaster do
|
4
|
+
subject { HashCast::Casters::IntegerCaster }
|
5
|
+
|
6
|
+
it "should cast from a numeric string" do
|
7
|
+
result = subject.cast("123", :number)
|
8
|
+
expect(result).to eq(123)
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should cast from an positive integer" do
|
12
|
+
result = subject.cast(10_000, :number)
|
13
|
+
expect(result).to eq(10_000)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should cast from a negative integer" do
|
17
|
+
result = subject.cast(-10_000, :number)
|
18
|
+
expect(result).to eq(-10_000)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should cast from a zero" do
|
22
|
+
result = subject.cast(0, :number)
|
23
|
+
expect(result).to eq(0)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should not cast from a random string" do
|
27
|
+
expect {
|
28
|
+
subject.cast("test", :number)
|
29
|
+
}.to raise_error(HashCast::Errors::CastingError, "is invalid integer")
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should not cast from an array" do
|
33
|
+
expect {
|
34
|
+
subject.cast([1], :number)
|
35
|
+
}.to raise_error(HashCast::Errors::CastingError, "should be a integer")
|
36
|
+
end
|
37
|
+
|
38
|
+
it "only allow values larger than max integer range" do
|
39
|
+
expect {
|
40
|
+
subject.cast(2**40, :number)
|
41
|
+
}.to raise_error(HashCast::Errors::CastingError, "should be within allowed range")
|
42
|
+
end
|
43
|
+
|
44
|
+
it "only allow values smaller than min integer range" do
|
45
|
+
expect {
|
46
|
+
subject.cast(2**40 * -1, :number)
|
47
|
+
}.to raise_error(HashCast::Errors::CastingError, "should be within allowed range")
|
48
|
+
end
|
49
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_cast
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Albert Gazizov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-
|
11
|
+
date: 2025-06-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -79,6 +79,7 @@ files:
|
|
79
79
|
- lib/hash_cast/version.rb
|
80
80
|
- spec/hash_cast/caster_spec.rb
|
81
81
|
- spec/hash_cast/casters/array_caster_spec.rb
|
82
|
+
- spec/hash_cast/casters/integer_caster_spec.rb
|
82
83
|
- spec/hash_cast/casters/string_caster_spec.rb
|
83
84
|
- spec/hash_cast/hash_cast_spec.rb
|
84
85
|
- spec/spec_helper.rb
|
@@ -108,6 +109,7 @@ summary: Declarative Hash Caster
|
|
108
109
|
test_files:
|
109
110
|
- spec/hash_cast/caster_spec.rb
|
110
111
|
- spec/hash_cast/casters/array_caster_spec.rb
|
112
|
+
- spec/hash_cast/casters/integer_caster_spec.rb
|
111
113
|
- spec/hash_cast/casters/string_caster_spec.rb
|
112
114
|
- spec/hash_cast/hash_cast_spec.rb
|
113
115
|
- spec/spec_helper.rb
|