module Her::Model::Attributes

This module handles all methods related to model attributes

Public Class Methods

initialize_collection(klass, parsed_data={}) click to toggle source

Initialize a collection of resources

@private

# File lib/her/model/attributes.rb, line 34
def self.initialize_collection(klass, parsed_data={})
  collection_data = klass.extract_array(parsed_data).map do |item_data|
    if item_data.kind_of?(klass)
      resource = item_data
    else
      resource = klass.new(klass.parse(item_data))
      resource.run_callbacks :find
    end
    resource
  end
  Her::Collection.new(collection_data, parsed_data[:metadata], parsed_data[:errors])
end
new(attributes={}) click to toggle source

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">
# File lib/her/model/attributes.rb, line 20
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)
  run_callbacks :initialize
end
use_setter_methods(model, params) click to toggle source

Use setter methods of model for each key / value pair in params Return key / value pairs for which no setter method was defined on the model

@private

# File lib/her/model/attributes.rb, line 51
def self.use_setter_methods(model, params)
  params ||= {}

  reserved_keys = [:id, model.class.primary_key] + model.class.association_keys
  model.class.attributes *params.keys.reject { |k| reserved_keys.include?(k) || reserved_keys.map(&:to_s).include?(k) }

  setter_method_names = model.class.setter_method_names
  params.inject({}) do |memo, (key, value)|
    setter_method = key.to_s + '='
    if setter_method_names.include?(setter_method)
      model.send(setter_method, value)
    else
      key = key.to_sym if key.is_a?(String)
      memo[key] = value
    end
    memo
  end
end

Public Instance Methods

==(other) click to toggle source

Return `true` if the other object is also a Her::Model and has matching data

@private

# File lib/her/model/attributes.rb, line 143
def ==(other)
  other.is_a?(Her::Model) && @attributes == other.attributes
end
assign_attributes(new_attributes) click to toggle source

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 103
def assign_attributes(new_attributes)
  @attributes ||= attributes
  # Use setter methods first
  unset_attributes = Her::Model::Attributes.use_setter_methods(self, new_attributes)

  # Then translate attributes of associations into association instances
  parsed_attributes = self.class.parse_associations(unset_attributes)

  # Then merge the parsed_data into @attributes.
  @attributes.merge!(parsed_attributes)
end
Also aliased as: attributes=
attribute(attribute_name)
Alias for: get_attribute
attributes() click to toggle source
# File lib/her/model/attributes.rb, line 116
def attributes
  @attributes ||= HashWithIndifferentAccess.new
end
attributes=(new_attributes)
Alias for: assign_attributes
eql?(other) click to toggle source

Delegate to the == method

@private

# File lib/her/model/attributes.rb, line 150
def eql?(other)
  self == other
end
get_attribute(attribute_name) click to toggle source

Handles returning data for a specific attribute

@private

# File lib/her/model/attributes.rb, line 130
def get_attribute(attribute_name)
  @attributes[attribute_name]
end
Also aliased as: attribute
has_attribute?(attribute_name) click to toggle source

Handles returning true for the accessible attributes

@private

# File lib/her/model/attributes.rb, line 123
def has_attribute?(attribute_name)
  @attributes.include?(attribute_name)
end
hash() click to toggle source

Delegate to @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 157
def hash
  @attributes.hash
end
id() click to toggle source

Return the value of the model `primary_key` attribute

# File lib/her/model/attributes.rb, line 136
def id
  @attributes[self.class.primary_key]
end
method_missing(method, *args, &blk) click to toggle source

Handles missing methods

@private

Calls superclass method
# File lib/her/model/attributes.rb, line 73
def method_missing(method, *args, &blk)
  if method.to_s =~ /[?=]$/ || @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
respond_to_missing?(method, include_private = false) click to toggle source

@private

Calls superclass method
# File lib/her/model/attributes.rb, line 89
def respond_to_missing?(method, include_private = false)
  method.to_s.end_with?('=') || method.to_s.end_with?('?') || @attributes.include?(method) || super
end