module Her::Model::Attributes
This module handles all methods related to model attributes
Public Class Methods
Initialize a new object with data
@param [Hash] attributes The attributes to initialize the object with @option attributes [Hash,Array] :_metadata @option attributes [Hash,Array] :_errors @option attributes [Boolean] :_destroyed
@example
class User include Her::Model end User.new(name: "Tobias") # => #<User name="Tobias"> User.new do |u| u.name = "Tobias" end # => #<User name="Tobias">
# File lib/her/model/attributes.rb, line 26 def initialize(attributes = {}) attributes ||= {} @metadata = attributes.delete(:_metadata) || {} @response_errors = attributes.delete(:_errors) || {} @destroyed = attributes.delete(:_destroyed) || false attributes = self.class.default_scope.apply_to(attributes) assign_attributes(attributes) yield self if block_given? run_callbacks :initialize end
Public Instance Methods
Return `true` if the other object is also a Her::Model
and has matching data
@private
# File lib/her/model/attributes.rb, line 127 def ==(other) other.is_a?(Her::Model) && @_her_attributes == other.attributes end
Assign new attributes to a resource
@example
class User include Her::Model end user = User.find(1) # => #<User id=1 name="Tobias"> user.assign_attributes(name: "Lindsay") user.changes # => { :name => ["Tobias", "Lindsay"] }
# File lib/her/model/attributes.rb, line 71 def assign_attributes(new_attributes) if !new_attributes.respond_to?(:to_hash) raise ArgumentError, "When assigning attributes, you must pass a hash as an argument." end # Coerce new_attributes to hash in case of strong parameters new_attributes = new_attributes.to_hash @_her_attributes ||= attributes # Use setter methods first unset_attributes = self.class.use_setter_methods(self, new_attributes) # Then translate attributes of associations into association instances associations = self.class.parse_associations(unset_attributes) # Then merge the associations into @_her_attributes. @_her_attributes.merge!(associations) end
Assign attribute value (ActiveModel convention method).
@private
# File lib/her/model/attributes.rb, line 148 def attribute=(attribute, value) @_her_attributes[attribute] = nil unless @_her_attributes.include?(attribute) send("#{attribute}_will_change!") unless value == @_her_attributes[attribute] @_her_attributes[attribute] = value end
Check attribute value to be present (ActiveModel convention method).
@private
# File lib/her/model/attributes.rb, line 157 def attribute?(attribute) @_her_attributes.include?(attribute) && @_her_attributes[attribute].present? end
# File lib/her/model/attributes.rb, line 92 def attributes # The natural choice of instance variable naming here would be # `@attributes`. Unfortunately that causes a naming clash when # used with `ActiveModel` version >= 5.2.0. # As of v5.2.0 `ActiveModel` checks to see if `ActiveRecord` # attributes exist, and assumes that if the instance variable # `@attributes` exists on the instance, it is because they are # `ActiveRecord` attributes. @_her_attributes ||= HashWithIndifferentAccess.new end
Delegate to the == method
@private
# File lib/her/model/attributes.rb, line 134 def eql?(other) self == other end
Handles returning data for a specific attribute
@private
# File lib/her/model/attributes.rb, line 113 def get_attribute(attribute_name) @_her_attributes[attribute_name] end
Handles returning true for the accessible attributes
@private
# File lib/her/model/attributes.rb, line 106 def has_attribute?(attribute_name) @_her_attributes.include?(attribute_name) end
Delegate to @_her_attributes, allowing models to act correctly in code like:
[ Model.find(1), Model.find(1) ].uniq # => [ Model.find(1) ]
@private
# File lib/her/model/attributes.rb, line 141 def hash @_her_attributes.hash end
Return the value of the model `primary_key` attribute
# File lib/her/model/attributes.rb, line 119 def id @_her_attributes[self.class.primary_key] end
Handles missing methods
@private
# File lib/her/model/attributes.rb, line 41 def method_missing(method, *args, &blk) if method.to_s =~ /[?=]$/ || @_her_attributes.include?(method) # Extract the attribute attribute = method.to_s.sub(/[?=]$/, '') # Create a new `attribute` methods set self.class.attributes(*attribute) # Resend the method! send(method, *args, &blk) else super end end
@private
# File lib/her/model/attributes.rb, line 57 def respond_to_missing?(method, include_private = false) method.to_s =~ /[?=]$/ || @_her_attributes.include?(method) || super end