Module: ModelAttribute::InstanceMethods
- Defined in:
- lib/model_attribute.rb
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #attributes ⇒ Object
-
#attributes_for_json ⇒ Object
Attributes suitable for serializing to a JSON string.
- #changes ⇒ Object
-
#changes_for_json ⇒ Object
Changed attributes suitable for serializing to a JSON string.
-
#inspect ⇒ Object
Includes the class name and all the attributes and their values.
- #read_attribute(name) ⇒ Object
- #set_attributes(attributes, can_set_private_attrs = false) ⇒ Object
- #write_attribute(name, value, type = nil) ⇒ Object
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
106 107 108 109 110 111 112 113 |
# File 'lib/model_attribute.rb', line 106 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 |
#attributes ⇒ Object
94 95 96 97 98 |
# File 'lib/model_attribute.rb', line 94 def attributes self.class.attributes.each_with_object({}) do |name, attributes| attributes[name] = read_attribute(name) end end |
#attributes_for_json ⇒ Object
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.
126 127 128 129 130 131 132 133 134 |
# File 'lib/model_attribute.rb', line 126 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 |
#changes ⇒ Object
116 117 118 |
# File 'lib/model_attribute.rb', line 116 def changes @changes ||= {} end |
#changes_for_json ⇒ Object
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.
144 145 146 147 148 149 150 151 152 |
# File 'lib/model_attribute.rb', line 144 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 |
#inspect ⇒ Object
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>”
156 157 158 159 160 161 |
# File 'lib/model_attribute.rb', line 156 def inspect attribute_string = self.class.attributes.map do |key| "#{key}: #{read_attribute(key).inspect}" end.join(', ') "#<#{self.class} #{attribute_string}>" end |
#read_attribute(name) ⇒ Object
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/model_attribute.rb', line 83 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 |
#set_attributes(attributes, can_set_private_attrs = false) ⇒ Object
100 101 102 103 104 |
# File 'lib/model_attribute.rb', line 100 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 |
#write_attribute(name, value, type = nil) ⇒ Object
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 |
# File 'lib/model_attribute.rb', line 57 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 |