提交 2c5a0dde 编写于 作者: Y Yves Senn

Merge pull request #18543 from henrik/integer_limit_or_default

Tiny: DRY default limit in ActiveRecord::Type::Integer
......@@ -3,6 +3,10 @@ module Type
class Integer < Value # :nodoc:
include Numeric
# Column storage size in bytes.
# 4 bytes means a MySQL int or Postgres integer as opposed to smallint etc.
DEFAULT_LIMIT = 4
def initialize(*)
super
@range = min_value...max_value
......@@ -38,12 +42,12 @@ def cast_value(value)
def ensure_in_range(value)
unless range.cover?(value)
raise RangeError, "#{value} is out of range for #{self.class} with limit #{limit || 4}"
raise RangeError, "#{value} is out of range for #{self.class} with limit #{limit || DEFAULT_LIMIT}"
end
end
def max_value
limit = self.limit || 4
limit = self.limit || DEFAULT_LIMIT
1 << (limit * 8 - 1) # 8 bits per byte with one bit for sign
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册