From 2e30327f4434d3e7099f70a4b0592f7efe35fac6 Mon Sep 17 00:00:00 2001 From: Ivan Chernov Date: Sun, 18 Aug 2019 18:17:54 +0300 Subject: [PATCH 1/4] Fix Integer convert bug, update bundler --- lib/tainbox/type_converter.rb | 2 +- spec/tainbox_spec.rb | 32 ++++++++++++++++++++++++++++++++ tainbox.gemspec | 2 +- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/lib/tainbox/type_converter.rb b/lib/tainbox/type_converter.rb index 668e8e4..eedcf7b 100644 --- a/lib/tainbox/type_converter.rb +++ b/lib/tainbox/type_converter.rb @@ -32,7 +32,7 @@ def convert end Tainbox.define_converter(Integer) do - Integer(value) rescue nil + value.is_a?(String) ? Integer(value, 10) : Integer(value) rescue nil end Tainbox.define_converter(Float) do diff --git a/spec/tainbox_spec.rb b/spec/tainbox_spec.rb index df45762..371190a 100644 --- a/spec/tainbox_spec.rb +++ b/spec/tainbox_spec.rb @@ -166,4 +166,36 @@ def name expect(oliver.name).to eq('John') end end + + describe 'convert to integer string with radix indicators' do + let(:person) do + Class.new do + include Tainbox + + attribute :age, Integer + end + end + + let(:attributes) { Hash[age: "014"] } + + it "uses decimal number system as base" do + expect(person.new(attributes).age).to eq(14) + end + end + + describe 'float number with Integer type' do + let(:person) do + Class.new do + include Tainbox + + attribute :age, Integer + end + end + + let(:attributes) { Hash[age: 14.2] } + + it "converts properly" do + expect(person.new(attributes).age).to eq(14) + end + end end diff --git a/tainbox.gemspec b/tainbox.gemspec index 52b18cf..372c3e9 100644 --- a/tainbox.gemspec +++ b/tainbox.gemspec @@ -19,7 +19,7 @@ Gem::Specification.new do |spec| spec.add_runtime_dependency 'activesupport' - spec.add_development_dependency 'bundler', '~> 1.8' + spec.add_development_dependency 'bundler', '~> 2.0' spec.add_development_dependency 'rake', '~> 10.0' spec.add_development_dependency 'pry' spec.add_development_dependency 'rspec' From a3692d34e44d28e17dd75484a3d3c8901c79f06b Mon Sep 17 00:00:00 2001 From: Ivan Chernov Date: Sun, 18 Aug 2019 18:19:39 +0300 Subject: [PATCH 2/4] Add info about Integer type --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f2f508c..15dd961 100644 --- a/README.md +++ b/README.md @@ -94,7 +94,7 @@ end All converters return nil if conversion could not be made. -- Integer +- Integer (*converts from strings with decimal base*) - Float - String - Symbol From 4b07cbbf3cb5f91c1791f6da4674fd96d302500b Mon Sep 17 00:00:00 2001 From: Ivan Chernov Date: Sun, 18 Aug 2019 18:20:52 +0300 Subject: [PATCH 3/4] Refactor writed specs --- README.md | 2 +- spec/tainbox_spec.rb | 26 +++++++++++--------------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 15dd961..971aae3 100644 --- a/README.md +++ b/README.md @@ -94,7 +94,7 @@ end All converters return nil if conversion could not be made. -- Integer (*converts from strings with decimal base*) +- Integer (*converts from strings using decimal base*) - Float - String - Symbol diff --git a/spec/tainbox_spec.rb b/spec/tainbox_spec.rb index 371190a..a013c4b 100644 --- a/spec/tainbox_spec.rb +++ b/spec/tainbox_spec.rb @@ -167,7 +167,7 @@ def name end end - describe 'convert to integer string with radix indicators' do + describe "Integer type" do let(:person) do Class.new do include Tainbox @@ -176,26 +176,22 @@ def name end end - let(:attributes) { Hash[age: "014"] } + let(:attributes) { Hash[age: age] } - it "uses decimal number system as base" do - expect(person.new(attributes).age).to eq(14) - end - end - - describe 'float number with Integer type' do - let(:person) do - Class.new do - include Tainbox + describe 'string with radix indicators' do + let(:age) { "014" } - attribute :age, Integer + it 'uses decimal number system as base' do + expect(person.new(attributes).age).to eq(14) end end - let(:attributes) { Hash[age: 14.2] } + describe 'float number' do + let(:age) { 14.2 } - it "converts properly" do - expect(person.new(attributes).age).to eq(14) + it 'converts properly' do + expect(person.new(attributes).age).to eq(14) + end end end end From 129f4d1cea402ad70b6bec53d81237e240babec9 Mon Sep 17 00:00:00 2001 From: Ivan Chernov Date: Sun, 18 Aug 2019 19:48:14 +0300 Subject: [PATCH 4/4] Add changelog and version bump --- CHANGELOG.md | 5 +++++ README.md | 2 +- lib/tainbox/version.rb | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ee97a27..693fb1f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -54,3 +54,8 @@ semantics ## 2.1.1 * Fix `Time` converter + +## 2.1.2 + +* Fix Integer type casting: Integer parses strings with leading zeros as numbers in octal number system +* Update version of bundler diff --git a/README.md b/README.md index 971aae3..fe67afa 100644 --- a/README.md +++ b/README.md @@ -94,7 +94,7 @@ end All converters return nil if conversion could not be made. -- Integer (*converts from strings using decimal base*) +- Integer (*string parsed as numbers in decimal number system*) - Float - String - Symbol diff --git a/lib/tainbox/version.rb b/lib/tainbox/version.rb index 9150c30..c2ab9ed 100644 --- a/lib/tainbox/version.rb +++ b/lib/tainbox/version.rb @@ -1,3 +1,3 @@ module Tainbox - VERSION = '2.1.2' + VERSION = '2.1.3' end