Module: Humanoid::Associations::ClassMethods

Defined in:
lib/humanoid/associations.rb

Instance Method Summary collapse

Instance Method Details

#belongs_to(name, options = {}, &block) ⇒ Object

Adds the association back to the parent document. This macro is necessary to set the references from the child back to the parent document. If a child does not define this association calling persistence methods on the child object will cause a save to fail.

Options:

name: A Symbol that matches the name of the parent class.

Example:

class Person
  include Humanoid::Document
  has_many :addresses
end

class Address
  include Humanoid::Document
  belongs_to :person, :inverse_of => :addresses
end


62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/humanoid/associations.rb', line 62

def belongs_to(name, options = {}, &block)
  unless options.has_key?(:inverse_of)
    raise Errors::InvalidOptions.new("Options for belongs_to association must include :inverse_of")
  end
  self.embedded = true
  add_association(
    Associations::BelongsTo,
    Associations::Options.new(
      options.merge(:name => name, :extend => block)
    )
  )
end

Adds a relational association from the child Document to a Document in another database or collection.

Options:

name: A Symbol that is the related class name.

Example:

class Game
  include Humanoid::Document
  belongs_to_related :person
end


89
90
91
92
93
94
95
96
97
98
# File 'lib/humanoid/associations.rb', line 89

def belongs_to_related(name, options = {}, &block)
  field "#{name.to_s}_id"
  index "#{name.to_s}_id" unless self.embedded
  add_association(
    Associations::BelongsToRelated,
    Associations::Options.new(
      options.merge(:name => name, :extend => block)
    )
  )
end

#has_many(name, options = {}, &block) ⇒ Object

Adds the association from a parent document to its children. The name of the association needs to be a pluralized form of the child class name.

Options:

name: A Symbol that is the plural child class name.

Example:

class Person
  include Humanoid::Document
  has_many :addresses
end

class Address
  include Humanoid::Document
  belongs_to :person, :inverse_of => :addresses
end


119
120
121
122
123
124
125
126
# File 'lib/humanoid/associations.rb', line 119

def has_many(name, options = {}, &block)
  add_association(
    Associations::HasMany,
    Associations::Options.new(
      options.merge(:name => name, :extend => block)
    )
  )
end

Adds a relational association from the Document to many Documents in another database or collection.

Options:

name: A Symbol that is the related class name pluralized.

Example:

class Person
  include Humanoid::Document
  has_many_related :posts
end


142
143
144
145
146
147
148
149
150
151
152
# File 'lib/humanoid/associations.rb', line 142

def has_many_related(name, options = {}, &block)
  add_association(
    Associations::HasManyRelated,
    Associations::Options.new(
      options.merge(:name => name, :parent_key => self.name.foreign_key, :extend => block)
    )
  )
  before_save do |document|
    document.update_associations(name)
  end
end

#has_one(name, options = {}, &block) ⇒ Object

Adds the association from a parent document to its child. The name of the association needs to be a singular form of the child class name.

Options:

name: A Symbol that is the plural child class name.

Example:

class Person
  include Humanoid::Document
  has_many :addresses
end

class Address
  include Humanoid::Document
  belongs_to :person
end


173
174
175
176
177
178
179
180
181
# File 'lib/humanoid/associations.rb', line 173

def has_one(name, options = {}, &block)
  opts = Associations::Options.new(
    options.merge(:name => name, :extend => block)
  )
  type = Associations::HasOne
  add_association(type, opts)
  add_builder(type, opts)
  add_creator(type, opts)
end

Adds a relational association from the Document to one Document in another database or collection.

Options:

name: A Symbol that is the related class name pluralized.

Example:

class Person
  include Humanoid::Document
  has_one_related :game
end


196
197
198
199
200
201
202
203
204
205
206
# File 'lib/humanoid/associations.rb', line 196

def has_one_related(name, options = {}, &block)
  add_association(
    Associations::HasOneRelated,
    Associations::Options.new(
      options.merge(:name => name, :parent_key => self.name.foreign_key, :extend => block)
    )
  )
  before_save do |document|
    document.update_association(name)
  end
end

#reflect_on_association(name) ⇒ Object

Returns the macro associated with the supplied association name. This will return has_one, has_many, belongs_to or nil.

Options:

name: The association name.

Example:

Person.reflect_on_association(:addresses)



218
219
220
221
# File 'lib/humanoid/associations.rb', line 218

def reflect_on_association(name)
  association = associations[name.to_s]
  association ? association.macro : nil
end