Sha256: e28c4b6cc5da3916f74b6953b6cfe37493768f416613e534109c59ed53cfd20e

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

module Restly::Associations::Base::Loaders

  def load(parent, options)

    # Merge Options
    options.reverse_merge!(self.options)

    # Authorize and Set Path
    association = authorize(options[:authorize]).with_path(parent, options[:path])

    # Load Collection or Instance
    collection? ? association.load_collection(parent) : association.load_instance(parent)
  end

  private

  def load_collection(parent, association_class = self.association_class)
    raise Restly::Error::AssociationError, "Not a collection" unless collection?
    collection = if embedded?
                   []
                 else
                   association_class.all
                 end

    Restly::Proxies::Associations::Collection.new(collection, parent)
  end

  def load_instance(parent, association_class = self.association_class)
    raise Restly::Error::AssociationError, "Not an instance" if collection?
    return nil if embedded?
    instance = if parent.attributes.has_key? "#{name}_id"
                 foreign_key = parent.attributes["#{name}_id"]
                 return nil unless foreign_key
                 association_class.find(foreign_key)
               else
                 association_class.instance_from_response association_class.connection.get(association_class.path)
               end
    Restly::Proxies::Associations::Instance.new(instance, parent)
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
restly-0.0.1.alpha.6 lib/restly/associations/base/loaders.rb