module Her::Model::Attributes::ClassMethods
Public Instance Methods
Create a mutex for dynamically generated attribute methods or use one defined by ActiveModel.
@private
# File lib/her/model/attributes.rb, line 236 def attribute_methods_mutex @attribute_methods_mutex ||= begin if generated_attribute_methods.respond_to? :mu_synchronize generated_attribute_methods else Mutex.new end end end
Define the attributes that will be used to track dirty attributes and validations
@param [Array] attributes @example
class User include Her::Model attributes :name, :email end
# File lib/her/model/attributes.rb, line 255 def attributes(*attributes) attribute_methods_mutex.synchronize do define_attribute_methods attributes end end
Define attribute method matchers to automatically define them using ActiveModel's define_attribute_methods.
@private
# File lib/her/model/attributes.rb, line 227 def define_attribute_method_matchers attribute_method_suffix '=' attribute_method_suffix '?' end
Initialize a collection of resources
@private
# File lib/her/model/attributes.rb, line 181 def instantiate_collection(klass, parsed_data = {}) records = klass.extract_array(parsed_data).map do |record| instantiate_record(klass, data: record) end Her::Collection.new(records, parsed_data[:metadata], parsed_data[:errors]) end
Initialize a single resource
@private
# File lib/her/model/attributes.rb, line 165 def instantiate_record(klass, parsed_data) if (record = parsed_data[:data]) && record.is_a?(klass) record else attributes = klass.parse(record).merge(_metadata: parsed_data[:metadata], _errors: parsed_data[:errors]) klass.new(attributes).tap do |record_instance| record_instance.send :clear_changes_information record_instance.run_callbacks :find end end end
Initialize a collection of resources with raw data from an HTTP
request
@param [Array] parsed_data @private
# File lib/her/model/attributes.rb, line 192 def new_collection(parsed_data) instantiate_collection(self, parsed_data) end
Initialize a new object with the “raw” parsed_data from the parsing middleware
@private
# File lib/her/model/attributes.rb, line 199 def new_from_parsed_data(parsed_data) instantiate_record(self, parsed_data) end
@private
# File lib/her/model/attributes.rb, line 290 def setter_method_names @_her_setter_method_names ||= begin instance_methods.each_with_object(Set.new) do |method, memo| memo << method.to_s if method.to_s.end_with?('=') end end end
Define the accessor in which the API
response metadata (obtained from the parsing middleware) will be stored
@param [Symbol] store_metadata
@example
class User include Her::Model store_metadata :server_data end
# File lib/her/model/attributes.rb, line 285 def store_metadata(value = nil) store_her_data(:metadata, value) end
Define the accessor in which the API
response errors (obtained from the parsing middleware) will be stored
@param [Symbol] store_response_errors
@example
class User include Her::Model store_response_errors :server_errors end
# File lib/her/model/attributes.rb, line 271 def store_response_errors(value = nil) store_her_data(:response_errors, value) 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/her/model/attributes.rb, line 208 def use_setter_methods(model, params = {}) reserved = [:id, model.class.primary_key, *model.class.association_keys] model.class.attributes *params.keys.reject { |k| reserved.include?(k) } setter_method_names = model.class.setter_method_names params.each_with_object({}) do |(key, value), memo| setter_method = "#{key}=" if setter_method_names.include?(setter_method) model.send setter_method, value else memo[key.to_sym] = value end end end
Private Instance Methods
@private
# File lib/her/model/attributes.rb, line 301 def store_her_data(name, value) class_eval <<-RUBY, __FILE__, __LINE__ + 1 if @_her_store_#{name} && value.present? remove_method @_her_store_#{name}.to_sym remove_method @_her_store_#{name}.to_s + '=' end @_her_store_#{name} ||= begin superclass.store_#{name} if superclass.respond_to?(:store_#{name}) end return @_her_store_#{name} unless value @_her_store_#{name} = value define_method(value) { @#{name} } define_method(value.to_s+'=') { |value| @#{name} = value } RUBY end