-
Notifications
You must be signed in to change notification settings - Fork 6
/
model_attribute.rb
163 lines (138 loc) · 4.69 KB
/
model_attribute.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
require "model_attribute/version"
require "model_attribute/casts"
require "model_attribute/errors"
require "time"
module ModelAttribute
SUPPORTED_TYPES = [:integer, :float, :boolean, :string, :time, :json]
def self.extended(base)
base.send(:include, InstanceMethods)
base.instance_variable_set('@attribute_names', [])
base.instance_variable_set('@attribute_types', {})
base.instance_variable_set('@attribute_defaults', {})
end
def attribute(name, type, opts = {})
name = name.to_sym
type = type.to_sym
raise UnsupportedTypeError.new(type) unless SUPPORTED_TYPES.include?(type)
@attribute_names << name
@attribute_types[name] = type
@attribute_defaults[name] = opts[:default] if opts.key?(:default)
self.class_eval(<<-CODE, __FILE__, __LINE__ + 1)
def #{name}=(value)
write_attribute(#{name.inspect}, value, #{type.inspect})
end
def #{name}
read_attribute(#{name.inspect})
end
def #{name}_changed?
!!changes[#{name.inspect}]
end
CODE
if type == :boolean
self.class_eval(<<-CODE, __FILE__, __LINE__ + 1)
def #{name}?
!!read_attribute(#{name.inspect})
end
CODE
end
end
def attributes
@attribute_names
end
def attribute_defaults
@attribute_defaults
end
module InstanceMethods
def write_attribute(name, value, type = nil)
name = name.to_sym
# Don't want to expose attribute types as a method on the class, so access
# via a back door.
type ||= self.class.instance_variable_get('@attribute_types')[name]
raise InvalidAttributeNameError.new(name) unless type
value = Casts.cast(value, type)
return if value == read_attribute(name)
if changes.has_key? name
original = changes[name].first
else
original = read_attribute(name)
end
if original == value
changes.delete(name)
else
changes[name] = [original, value]
end
instance_variable_set("@#{name}", value)
end
def read_attribute(name)
ivar_name = "@#{name}"
if instance_variable_defined?(ivar_name)
instance_variable_get(ivar_name)
elsif !self.class.attributes.include?(name.to_sym)
raise InvalidAttributeNameError.new(name)
else
self.class.attribute_defaults[name.to_sym]
end
end
def attributes
self.class.attributes.each_with_object({}) do |name, attributes|
attributes[name] = read_attribute(name)
end
end
def set_attributes(attributes, can_set_private_attrs = false)
attributes.each do |key, value|
send("#{key}=", value) if respond_to?("#{key}=", can_set_private_attrs)
end
end
def ==(other)
return true if equal?(other)
if respond_to?(:id)
other.kind_of?(self.class) && id == other.id
else
other.kind_of?(self.class) && attributes == other.attributes
end
end
alias_method :eql?, :==
def changes
@changes ||= {}
end
# Attributes suitable for serializing to a JSON string.
#
# - Attribute keys are strings (for 'strict' JSON dumping).
# - Attributes with a default or nil value are omitted to speed serialization.
# - :time attributes are serialized as an Integer giving the number of
# milliseconds since the epoch.
def attributes_for_json
self.class.attributes.each_with_object({}) do |name, attributes|
value = read_attribute(name)
if value != self.class.attribute_defaults[name.to_sym]
value = (value.to_f * 1000).to_i if value.is_a? Time
attributes[name.to_s] = value
end
end
end
# Changed attributes suitable for serializing to a JSON string. Returns a
# hash from attribute name (as a string) to the new value of that attribute,
# for attributes that have changed.
#
# - :time attributes are serialized as an Integer giving the number of
# milliseconds since the epoch.
# - Unlike attributes_for_json, attributes that have changed to a nil value
# *are* included.
def changes_for_json
hash = {}
changes.each do |attr_name, (_old_value, new_value)|
new_value = (new_value.to_f * 1000).to_i if new_value.is_a? Time
hash[attr_name.to_s] = new_value
end
hash
end
# Includes the class name and all the attributes and their values. e.g.
# "#<User id: 1, paid: true, name: \"Fred\", created_at: 2014-12-25 08:00:00 +0000>"
def inspect
attribute_string = self.class.attributes.map do |key|
"#{key}: #{read_attribute(key).inspect}"
end.join(', ')
"#<#{self.class} #{attribute_string}>"
end
end
end