module Her::Model::Attributes
This module handles all methods related to model attributes
Public Class Methods
Initialize a collection of resources
@private
# File lib/castle-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
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/castle-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 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/castle-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
Return `true` if the other object is also a Her::Model
and has matching data
@private
# File lib/castle-her/model/attributes.rb, line 143 def ==(other) other.is_a?(Her::Model) && @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/castle-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
Assign attribute value (ActiveModel convention method).
@private
# File lib/castle-her/model/attributes.rb, line 164 def attribute=(attribute, value) @attributes[attribute] = nil unless @attributes.include?(attribute) self.send(:"#{attribute}_will_change!") if @attributes[attribute] != value @attributes[attribute] = value end
Check attribute value to be present (ActiveModel convention method).
@private
# File lib/castle-her/model/attributes.rb, line 173 def attribute?(attribute) @attributes.include?(attribute) && @attributes[attribute].present? end
# File lib/castle-her/model/attributes.rb, line 116 def attributes @attributes ||= HashWithIndifferentAccess.new end
Delegate to the == method
@private
# File lib/castle-her/model/attributes.rb, line 150 def eql?(other) self == other end
Handles returning data for a specific attribute
@private
# File lib/castle-her/model/attributes.rb, line 130 def get_attribute(attribute_name) @attributes[attribute_name] end
Handles returning true for the accessible attributes
@private
# File lib/castle-her/model/attributes.rb, line 123 def has_attribute?(attribute_name) @attributes.include?(attribute_name) end
Delegate to @attributes, allowing models to act correctly in code like:
[ Model.find(1), Model.find(1) ].uniq # => [ Model.find(1) ]
@private
# File lib/castle-her/model/attributes.rb, line 157 def hash @attributes.hash end
Return the value of the model `primary_key` attribute
# File lib/castle-her/model/attributes.rb, line 136 def id @attributes[self.class.primary_key] end
Handles missing methods
@private
# File lib/castle-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
@private
# File lib/castle-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