Browse files

Merge pull request #111 from greyblake/107_integer_coercion

Fix integer coercion bug

Fixes #107
  • Loading branch information...
2 parents defcb2d + be7fc9f commit 74db5b43d5f6b5960ae167f7ee08b6ff0ae09337 @solnic committed Sep 1, 2012
View
12 lib/virtus/coercion/string.rb
@@ -133,10 +133,14 @@ def self.to_boolean(value)
#
# @api public
def self.to_integer(value)
- # coerce to a Float first to evaluate scientific notation (if any)
- # that may change the integer part, then convert to an integer
- coerced = to_float(value)
- ::Float === coerced ? coerced.to_i : coerced
+ if value =~ /\A#{INTEGER_REGEXP}\z/
+ value.to_i
+ else
+ # coerce to a Float first to evaluate scientific notation (if any)
+ # that may change the integer part, then convert to an integer
+ coerced = to_float(value)
+ ::Float === coerced ? coerced.to_i : coerced
+ end
end
# Coerce value to float
View
6 spec/unit/virtus/coercion/string/class_methods/to_integer_spec.rb
@@ -47,4 +47,10 @@
it { should equal(string) }
end
+
+ context 'when integer string is big' do
+ let(:string) { '334490140000101135' }
+
+ it { should == 334490140000101135 }
+ end
end

0 comments on commit 74db5b4

Please sign in to comment.