module Her::Model::Associations::ClassMethods

Public Instance Methods

association_keys() click to toggle source

@private

# File lib/her/model/associations.rb, line 45
def association_keys
  associations.inject([]) { |memo, (_, details)| memo << details }.flatten.map { |a| a[:data_key] }
end
association_names() click to toggle source

@private

# File lib/her/model/associations.rb, line 40
def association_names
  associations.inject([]) { |memo, (_, details)| memo << details }.flatten.map { |a| a[:name] }
end
associations() click to toggle source

Return @_her_associations, lazily initialized with copy of the superclass' her_associations, or an empty hash.

@private

# File lib/her/model/associations.rb, line 33
def associations
  @_her_associations ||= begin
    superclass.respond_to?(:associations) ? superclass.associations.dup : Hash.new { |h, k| h[k] = [] }
  end
end
belongs_to(name, opts = {}) click to toggle source

Define a belongs_to association.

@param [Symbol] name The name of the method added to resources @param [Hash] opts Options @option opts [String] :class_name The name of the class to map objects to @option opts [Symbol] :data_key The attribute where the data is stored @option opts [Path] :path The relative path where to fetch the data @option opts [Symbol] :foreign_key The foreign key used to build the `:id` part of the path (defaults to `{name}_id`)

@example

class User
  include Her::Model
  belongs_to :team, :class_name => "Group"
end

class Group
  include Her::Model
end

@user = User.find(1) # => #<User(users/1) id=1 team_id=2 name="Tobias">
@user.team # => #<Team(teams/2) id=2 name="Developers">
# Fetched via GET "/teams/2"
# File lib/her/model/associations.rb, line 135
def belongs_to(name, opts = {})
  Her::Model::Associations::BelongsToAssociation.attach(self, name, opts)
end
has_many(name, opts = {}) click to toggle source

Define an has_many association.

@param [Symbol] name The name of the method added to resources @param [Hash] opts Options @option opts [String] :class_name The name of the class to map objects to @option opts [Symbol] :data_key The attribute where the data is stored @option opts [Path] :path The relative path where to fetch the data (defaults to `/{name}`)

@example

class User
  include Her::Model
  has_many :articles
end

class Article
  include Her::Model
end

@user = User.find(1)
@user.articles # => [#<Article(articles/2) id=2 title="Hello world.">]
# Fetched via GET "/users/1/articles"
# File lib/her/model/associations.rb, line 84
def has_many(name, opts = {})
  Her::Model::Associations::HasManyAssociation.attach(self, name, opts)
end
has_one(name, opts = {}) click to toggle source

Define an has_one association.

@param [Symbol] name The name of the method added to resources @param [Hash] opts Options @option opts [String] :class_name The name of the class to map objects to @option opts [Symbol] :data_key The attribute where the data is stored @option opts [Path] :path The relative path where to fetch the data (defaults to `/{name}`)

@example

class User
  include Her::Model
  has_one :organization
end

class Organization
  include Her::Model
end

@user = User.find(1)
@user.organization # => #<Organization(organizations/2) id=2 name="Foobar Inc.">
# Fetched via GET "/users/1/organization"
# File lib/her/model/associations.rb, line 109
def has_one(name, opts = {})
  Her::Model::Associations::HasOneAssociation.attach(self, name, opts)
end
parse_associations(data) click to toggle source

Parse associations data after initializing a new object

@private

# File lib/her/model/associations.rb, line 52
def parse_associations(data)
  associations.each_pair do |type, definitions|
    definitions.each do |association|
      association_class = "her/model/associations/#{type}_association".classify.constantize
      data.merge! association_class.parse(association, self, data)
    end
  end

  data
end