Skip to content

Commit

Permalink
DRY default limit in ActiveRecord::Type::Integer
Browse files Browse the repository at this point in the history
  • Loading branch information
henrik committed Jan 15, 2015
1 parent 39f0ea1 commit 1be562d
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions activerecord/lib/active_record/type/integer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 1be562d

Please sign in to comment.