aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/type/integer.rb
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2014-10-31 13:25:16 -0600
committerSean Griffin <sean@thoughtbot.com>2014-10-31 13:25:16 -0600
commit6af9411c2d49b87906b0d138f5213070144a2f6d (patch)
treee68f87d09cf60a11fffcbcbad8a91ed6bf0f36a5 /activerecord/lib/active_record/type/integer.rb
parentaeb431a6ca7cd05f8d29f454a24bf8f5bd5423ae (diff)
downloadrails-6af9411c2d49b87906b0d138f5213070144a2f6d.tar.gz
rails-6af9411c2d49b87906b0d138f5213070144a2f6d.tar.bz2
rails-6af9411c2d49b87906b0d138f5213070144a2f6d.zip
Use the correct values for int max and min
We had accidentally gone one power of two too far. In addition, we need to handle minimum values as well as the maximum.
Diffstat (limited to 'activerecord/lib/active_record/type/integer.rb')
-rw-r--r--activerecord/lib/active_record/type/integer.rb22
1 files changed, 13 insertions, 9 deletions
diff --git a/activerecord/lib/active_record/type/integer.rb b/activerecord/lib/active_record/type/integer.rb
index 2b0f0b2734..d69e5b3f28 100644
--- a/activerecord/lib/active_record/type/integer.rb
+++ b/activerecord/lib/active_record/type/integer.rb
@@ -3,12 +3,21 @@ module ActiveRecord
class Integer < Value # :nodoc:
include Numeric
+ def initialize(*)
+ super
+ @range = -max_value...max_value
+ end
+
def type
:integer
end
alias type_cast_for_database type_cast
+ protected
+
+ attr_reader :range
+
private
def cast_value(value)
@@ -17,25 +26,20 @@ module ActiveRecord
when false then 0
else
result = value.to_i rescue nil
- ensure_below_max(result) if result
+ ensure_in_range(result) if result
result
end
end
- def ensure_below_max(value)
- if value > max_value
+ def ensure_in_range(value)
+ unless range.cover?(value)
raise RangeError, "#{value} is too large for #{self.class} with limit #{limit || 4}"
end
end
def max_value
- @max_value = determine_max_value unless defined?(@max_value)
- @max_value
- end
-
- def determine_max_value
limit = self.limit || 4
- 2 << (limit * 8 - 1) # 8 bits per byte with one bit for sign
+ 1 << (limit * 8 - 1) # 8 bits per byte with one bit for sign
end
end
end