From 61b02bba81229069ebd06d4926fbf0cb6987d2dc Mon Sep 17 00:00:00 2001 From: Alex Zavgorodnev Date: Fri, 31 Jan 2025 19:44:14 +0300 Subject: [PATCH 1/3] feat: Added strong integer type --- README.md | 2 +- lib/anyway/type_casting.rb | 4 ++++ spec/type_casting_spec.rb | 2 ++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ec9fe919..20fab56d 100644 --- a/README.md +++ b/README.md @@ -711,7 +711,7 @@ CoolConfig.new.port == "443" #=> true **IMPORTANT**: Values provided explicitly (via attribute writers) are not coerced. Coercion is only happening during the load phase. -The following types are supported out-of-the-box: `:string`, `:integer`, `:float`, `:date`, `:datetime`, `:uri`, `:boolean`. +The following types are supported out-of-the-box: `:string`, `:integer`, `:integer!`, `:float`, `:date`, `:datetime`, `:uri`, `:boolean`. You can use custom deserializers by passing a callable object instead of a type name: diff --git a/lib/anyway/type_casting.rb b/lib/anyway/type_casting.rb index b4c77953..edbe6f94 100644 --- a/lib/anyway/type_casting.rb +++ b/lib/anyway/type_casting.rb @@ -88,6 +88,10 @@ def dup obj.accept(:boolean) do _1.to_s.match?(/\A(true|t|yes|y|1)\z/i) end + + obj.accept(:integer!) do + Integer(_1) + end end unless "".respond_to?(:safe_constantize) diff --git a/spec/type_casting_spec.rb b/spec/type_casting_spec.rb index 16c87970..09fa073b 100644 --- a/spec/type_casting_spec.rb +++ b/spec/type_casting_spec.rb @@ -8,6 +8,8 @@ specify "default types" do expect(casting.deserialize("12", :string)).to eq("12") expect(casting.deserialize("12.3", :integer)).to eq(12) + expect(casting.deserialize("12", :integer!)).to eq(12) + expect { casting.deserialize("12.3", :integer!) }.to raise_error(ArgumentError, /invalid value for Integer()/) expect(casting.deserialize("12.3", :float)).to eq(12.3) expect(casting.deserialize("2020-08-30 17:01:03", :date)).to eq(Date.parse("2020-08-30")) expect(casting.deserialize(Time.local(2020, 8, 30, 11, 44, 22), :date)).to eq(Date.parse("2020-08-30")) From 961d52bbe87ec3aadae6c412467d99ca717d3a2b Mon Sep 17 00:00:00 2001 From: Alex Zavgorodnev Date: Fri, 31 Jan 2025 19:54:24 +0300 Subject: [PATCH 2/3] change test --- spec/type_casting_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/type_casting_spec.rb b/spec/type_casting_spec.rb index 09fa073b..3ec5f85b 100644 --- a/spec/type_casting_spec.rb +++ b/spec/type_casting_spec.rb @@ -9,7 +9,7 @@ expect(casting.deserialize("12", :string)).to eq("12") expect(casting.deserialize("12.3", :integer)).to eq(12) expect(casting.deserialize("12", :integer!)).to eq(12) - expect { casting.deserialize("12.3", :integer!) }.to raise_error(ArgumentError, /invalid value for Integer()/) + expect { casting.deserialize("f", :integer!) }.to raise_error(ArgumentError, /invalid value for Integer()/) expect(casting.deserialize("12.3", :float)).to eq(12.3) expect(casting.deserialize("2020-08-30 17:01:03", :date)).to eq(Date.parse("2020-08-30")) expect(casting.deserialize(Time.local(2020, 8, 30, 11, 44, 22), :date)).to eq(Date.parse("2020-08-30")) From 9f340596bf2f5481a7a7af1336e77d9010a13521 Mon Sep 17 00:00:00 2001 From: Vladimir Dementyev Date: Tue, 11 Feb 2025 18:29:55 -0800 Subject: [PATCH 3/3] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 20fab56d..f38f8575 100644 --- a/README.md +++ b/README.md @@ -711,7 +711,7 @@ CoolConfig.new.port == "443" #=> true **IMPORTANT**: Values provided explicitly (via attribute writers) are not coerced. Coercion is only happening during the load phase. -The following types are supported out-of-the-box: `:string`, `:integer`, `:integer!`, `:float`, `:date`, `:datetime`, `:uri`, `:boolean`. +The following types are supported out-of-the-box: `:string`, `:integer`, `:integer!` (strict integer), `:float`, `:date`, `:datetime`, `:uri`, `:boolean`. You can use custom deserializers by passing a callable object instead of a type name: