class Her::Model::Associations::Association

Attributes

params[RW]

@private

Public Class Methods

new(parent, opts = {}) click to toggle source

@private

# File lib/castle-her/model/associations/association.rb, line 9
def initialize(parent, opts = {})
  @parent = parent
  @opts = opts
  @params = {}

  @klass = @parent.class.her_nearby_class(@opts[:class_name])
  @name = @opts[:name]
end
parse_single(association, klass, data) click to toggle source

@private

# File lib/castle-her/model/associations/association.rb, line 24
def self.parse_single(association, klass, data)
  data_key = association[:data_key]
  return {} unless data[data_key]

  klass = klass.her_nearby_class(association[:class_name])
  if data[data_key].kind_of?(klass)
    { association[:name] => data[data_key] }
  else
    { association[:name] => klass.new(klass.parse(data[data_key])) }
  end
end
proxy(parent, opts = {}) click to toggle source

@private

# File lib/castle-her/model/associations/association.rb, line 19
def self.proxy(parent, opts = {})
  AssociationProxy.new new(parent, opts)
end

Public Instance Methods

all(params = {})
Alias for: where
assign_single_nested_attributes(attributes) click to toggle source

@private

# File lib/castle-her/model/associations/association.rb, line 37
def assign_single_nested_attributes(attributes)
  if @parent.attributes[@name].blank?
    @parent.attributes[@name] = @klass.new(@klass.parse(attributes))
  else
    @parent.attributes[@name].assign_attributes(attributes)
  end
end
build_association_path(code) click to toggle source

@private

# File lib/castle-her/model/associations/association.rb, line 60
def build_association_path(code)
  begin
    instance_exec(&code)
  rescue Her::Errors::PathError
    return nil
  end
end
fetch(opts = {}) click to toggle source

@private

# File lib/castle-her/model/associations/association.rb, line 46
def fetch(opts = {})
  attribute_value = @parent.attributes[@name]
  return @opts[:default].try(:dup) if @parent.attributes.include?(@name) && (attribute_value.nil? || !attribute_value.nil? && attribute_value.empty?) && @params.empty?

  return @cached_result unless @params.any? || @cached_result.nil?
  return @parent.attributes[@name] unless @params.any? || @parent.attributes[@name].blank?

  path = build_association_path lambda { "#{@parent.request_path(@params)}#{@opts[:path]}" }
  @klass.get(path, @params).tap do |result|
    @cached_result = result unless @params.any?
  end
end
find(id) click to toggle source

Fetches the data specified by id

@example

class User
  include Her::Model
  has_many :comments
end

user = User.find(1)
user.comments.find(3) # Fetched via GET "/users/1/comments/3
# File lib/castle-her/model/associations/association.rb, line 94
def find(id)
  return nil if id.blank?
  path = build_association_path lambda { "#{@parent.request_path(@params)}#{@opts[:path]}/#{id}" }
  @klass.get_resource(path, @params)
end
where(params = {}) click to toggle source

Add query parameters to the HTTP request performed to fetch the data

@example

class User
  include Her::Model
  has_many :comments
end

user = User.find(1)
user.comments.where(:approved => 1) # Fetched via GET "/users/1/comments?approved=1
# File lib/castle-her/model/associations/association.rb, line 78
def where(params = {})
  return self if params.blank? && @parent.attributes[@name].blank?
  AssociationProxy.new self.clone.tap { |a| a.params = a.params.merge(params) }
end
Also aliased as: all