module Her::Model::ORM

This module adds ORM-like capabilities to the model

Attributes

attributes[RW]
attributes=[RW]
data[RW]
errors[RW]
metadata[RW]

Public Class Methods

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

Initialize a collection of resources @private

# File lib/her/model/orm.rb, line 53
def self.initialize_collection(klass, parsed_data={})
  collection_data = parsed_data[:data].map do |item_data|
    resource = klass.new(klass.parse(item_data))
    klass.wrap_in_hooks(resource, :find)
    resource
  end

  collection_class = Her::Collection
  if parsed_data[:metadata].is_a?(Hash) && parsed_data[:metadata].has_key?(:current_page)
    collection_class = Her::PaginatedCollection
  end
  collection_class.new(collection_data, parsed_data[:metadata], parsed_data[:errors])
end
new(params={}) click to toggle source

Initialize a new object with data received from an HTTP request

# File lib/her/model/orm.rb, line 23
def initialize(params={})
  fields = self.class.instance_variable_defined?(:@fields) ? self.class.instance_variable_get(:@fields) : []
  @data = Hash[fields.map{ |field| [field, nil] }]
  @metadata = params.delete(:_metadata) || {}
  @errors = params.delete(:_errors) || {}

  # Use setter methods first, then translate attributes of relationships
  # into relationship instances, then merge the parsed_data into @data.
  unset_data = Her::Model::ORM.use_setter_methods(self, params)
  parsed_data = self.class.parse_relationships(unset_data)
  @data.update(convert_types(parsed_data))
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/orm.rb, line 112
def self.use_setter_methods(model, params)
  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
      if key.is_a?(String)
        key = key.to_sym
      end
      memo[key] = value
    end
    memo
  end
end

Public Instance Methods

assign_attributes(new_data)
Alias for: assign_data
assign_data(new_data) click to toggle source

Assign new data to an instance

# File lib/her/model/orm.rb, line 87
def assign_data(new_data)
  new_data = Her::Model::ORM.use_setter_methods(self, new_data)
  @data.update new_data
end
Also aliased as: assign_attributes
convert_types(parsed_data) click to toggle source
# File lib/her/model/orm.rb, line 36
def convert_types(parsed_data)
  parsed_data.each do |key, value|
    "2013-01-26T09:29:39.358Z"
    if value.is_a?(String)
      m = value.match(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?Z/)
      next if m.nil?
      if m[1].nil?
        parsed_data[key] = Time.strptime(value, '%Y-%m-%dT%H:%M:%S%Z')
      else
        parsed_data[key] = Time.strptime(value, '%Y-%m-%dT%H:%M:%S.%L%Z')
      end
    end
  end
end
get_data(attribute_name) click to toggle source

Handles returning attribute value from data

# File lib/her/model/orm.rb, line 99
def get_data(attribute_name)
  @data[attribute_name]
end
has_data?(attribute_name) click to toggle source

Handles returning true for the accessible attributes

# File lib/her/model/orm.rb, line 94
def has_data?(attribute_name)
  @data.include?(attribute_name)
end
id() click to toggle source

Override the method to prevent from returning the object ID (in ruby-1.8.7) @private

Calls superclass method
# File lib/her/model/orm.rb, line 105
def id
  @data[:id] || super
end
method_missing(method, *args, &blk) click to toggle source

Handles missing methods by routing them through @data @private

Calls superclass method
# File lib/her/model/orm.rb, line 69
def method_missing(method, *args, &blk)
  if method.to_s.end_with?('=')
    @data[method.to_s.chomp('=').to_sym] = args.first
  elsif method.to_s.end_with?('?')
    @data.include?(method.to_s.chomp('?').to_sym)
  elsif @data.include?(method)
    @data[method]
  else
    super
  end
end
respond_to?(method, include_private = false) click to toggle source

Handles returning true for the cases handled by method_missing

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