-
Notifications
You must be signed in to change notification settings - Fork 21.4k
/
integer.rb
113 lines (98 loc) · 2.74 KB
/
integer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# frozen_string_literal: true
module ActiveModel
module Type
# = Active Model \Integer \Type
#
# Attribute type for integer representation. This type is registered under
# the +:integer+ key.
#
# class Person
# include ActiveModel::Attributes
#
# attribute :age, :integer
# end
#
# Values are cast using their +to_i+ method, except for blank strings, which
# are cast to +nil+. If a +to_i+ method is not defined or raises an error,
# the value will be cast to +nil+.
#
# person = Person.new
#
# person.age = "18"
# person.age # => 18
#
# person.age = ""
# person.age # => nil
#
# person.age = :not_an_integer
# person.age # => nil (because Symbol does not define #to_i)
#
# Serialization also works under the same principle. Non-numeric strings are
# serialized as +nil+, for example.
#
# Serialization also validates that the integer can be stored using a
# limited number of bytes. If it cannot, an ActiveModel::RangeError will be
# raised. The default limit is 4 bytes, and can be customized when declaring
# an attribute:
#
# class Person
# include ActiveModel::Attributes
#
# attribute :age, :integer, limit: 6
# end
class Integer < Value
include Helpers::Numeric
# Column storage size in bytes.
# 4 bytes means an integer as opposed to smallint etc.
DEFAULT_LIMIT = 4
def initialize(**)
super
@range = min_value...max_value
end
def type
:integer
end
def deserialize(value)
return if value.blank?
value.to_i
end
def serialize(value)
return if value.is_a?(::String) && non_numeric_string?(value)
ensure_in_range(super)
end
def serialize_cast_value(value) # :nodoc:
ensure_in_range(value)
end
def serializable?(value)
cast_value = cast(value)
in_range?(cast_value) || begin
yield cast_value if block_given?
false
end
end
private
attr_reader :range
def in_range?(value)
!value || range.member?(value)
end
def cast_value(value)
value.to_i rescue nil
end
def ensure_in_range(value)
unless in_range?(value)
raise ActiveModel::RangeError, "#{value} is out of range for #{self.class} with limit #{_limit} bytes"
end
value
end
def max_value
1 << (_limit * 8 - 1) # 8 bits per byte with one bit for sign
end
def min_value
-max_value
end
def _limit
limit || DEFAULT_LIMIT
end
end
end
end