Skip to content
This repository has been archived by the owner on Jun 2, 2021. It is now read-only.

Fix converting in Integer type #26

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ end

All converters return nil if conversion could not be made.

- Integer
- Integer (*string parsed as numbers in decimal number system*)
- Float
- String
- Symbol
Expand Down
2 changes: 1 addition & 1 deletion lib/tainbox/type_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/tainbox/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Tainbox
VERSION = '2.1.2'
VERSION = '2.1.3'
end
28 changes: 28 additions & 0 deletions spec/tainbox_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,32 @@ def name
expect(oliver.name).to eq('John')
end
end

describe "Integer type" do
let(:person) do
Class.new do
include Tainbox

attribute :age, Integer
end
end

let(:attributes) { Hash[age: age] }

describe 'string with radix indicators' do
let(:age) { "014" }

it 'uses decimal number system as base' do
expect(person.new(attributes).age).to eq(14)
end
end

describe 'float number' do
let(:age) { 14.2 }

it 'converts properly' do
expect(person.new(attributes).age).to eq(14)
end
end
end
end
2 changes: 1 addition & 1 deletion tainbox.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down