module Her::Model::Attributes::ClassMethods

Public Instance Methods

attribute_methods_mutex() click to toggle source

Create a mutex for dynamically generated attribute methods or use one defined by ActiveModel.

@private

# File lib/her/model/attributes.rb, line 218
def attribute_methods_mutex
  @attribute_methods_mutex ||= if generated_attribute_methods.respond_to? :mu_synchronize
                                 generated_attribute_methods
                               else
                                 Mutex.new
                               end
end
attributes(*attributes) click to toggle source

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 234
def attributes(*attributes)
  attribute_methods_mutex.synchronize do
    define_attribute_methods attributes
  end
end
define_attribute_method_matchers() click to toggle source

Define attribute method matchers to automatically define them using ActiveModel's define_attribute_methods.

@private

# File lib/her/model/attributes.rb, line 210
def define_attribute_method_matchers
  attribute_method_suffix '='
  attribute_method_suffix '?'
end
new_collection(parsed_data) click to toggle source

Initialize a collection of resources with raw data from an HTTP request

@param [Array] parsed_data @private

# File lib/her/model/attributes.rb, line 195
def new_collection(parsed_data)
  Her::Model::Attributes.initialize_collection(self, parsed_data)
end
new_from_parsed_data(parsed_data) click to toggle source

Initialize a new object with the “raw” parsed_data from the parsing middleware

@private

# File lib/her/model/attributes.rb, line 202
def new_from_parsed_data(parsed_data)
  parsed_data = parsed_data.with_indifferent_access
  new(parse(parsed_data[:data]).merge :_metadata => parsed_data[:metadata], :_errors => parsed_data[:errors])
end
setter_method_names() click to toggle source

@private

# File lib/her/model/attributes.rb, line 267
def setter_method_names
  @_her_setter_method_names ||= instance_methods.inject(Set.new) do |memo, method_name|
    memo << method_name.to_s if method_name.to_s.end_with?('=')
    memo
  end
end
store_metadata(value = nil) click to toggle source

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 262
def store_metadata(value = nil)
  store_her_data(:metadata, value)
end
store_response_errors(value = nil) click to toggle source

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 249
def store_response_errors(value = nil)
  store_her_data(:response_errors, value)
end

Private Instance Methods

store_her_data(name, value) click to toggle source

@private

# File lib/her/model/attributes.rb, line 276
        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