class ActiveModel::Type::Integer
:nodoc:
def _limit
def _limit limit || DEFAULT_LIMIT end
def cast_value(value)
def cast_value(value) case value when true then 1 when false then 0 else value.to_i rescue nil end end
def deserialize(value)
def deserialize(value) return if value.nil? value.to_i end
def ensure_in_range(value)
def ensure_in_range(value) unless range.cover?(value) raise ActiveModel::RangeError, "#{value} is out of range for #{self.class} with limit #{_limit} bytes" end end
def initialize(*)
def initialize(*) super @range = min_value...max_value end
def max_value
def max_value 1 << (_limit * 8 - 1) # 8 bits per byte with one bit for sign end
def min_value
def min_value -max_value end
def serialize(value)
def serialize(value) result = cast(value) if result ensure_in_range(result) end result end
def type
def type :integer end