Module: MongoMapper::Plugins::Associations::ClassMethods

Defined in:
lib/mongo_mapper/plugins/associations.rb

Instance Method Summary collapse

Instance Method Details

#associationsObject



25
26
27
# File 'lib/mongo_mapper/plugins/associations.rb', line 25

def associations
  @associations ||= {}
end

#associations=(hash) ⇒ Object



29
30
31
32
# File 'lib/mongo_mapper/plugins/associations.rb', line 29

def associations=(hash)
  @embedded_associations = nil
  @associations = hash
end

#associations_moduleObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/mongo_mapper/plugins/associations.rb', line 48

def associations_module
  if associations_module_defined?
    const_get 'MongoMapperAssociations'
  else
    Module.new.tap do |m|
      const_set 'MongoMapperAssociations', m
      include m
    end
  end
end

#associations_module_defined?Boolean

Returns:



38
39
40
41
42
43
44
45
46
# File 'lib/mongo_mapper/plugins/associations.rb', line 38

def associations_module_defined?
  # :nocov:
  if method(:const_defined?).arity == 1 # Ruby 1.9 compat check
    const_defined?('MongoMapperAssociations')
  else
    const_defined?('MongoMapperAssociations', false)
  end
  # :nocov:
end

#belongs_to(association_id, options = {}, &extension) ⇒ Object



13
14
15
# File 'lib/mongo_mapper/plugins/associations.rb', line 13

def belongs_to(association_id, options={}, &extension)
  create_association(BelongsToAssociation.new(association_id, options, &extension))
end

#embedded_associationsObject



34
35
36
# File 'lib/mongo_mapper/plugins/associations.rb', line 34

def embedded_associations
  @embedded_associations ||= associations.values.select { |assoc| assoc.embeddable? }
end

#inherited(subclass) ⇒ Object



8
9
10
11
# File 'lib/mongo_mapper/plugins/associations.rb', line 8

def inherited(subclass)
  subclass.associations = associations.dup
  super
end

#many(association_id, options = {}, &extension) ⇒ Object



17
18
19
# File 'lib/mongo_mapper/plugins/associations.rb', line 17

def many(association_id, options={}, &extension)
  create_association(ManyAssociation.new(association_id, options, &extension))
end

#one(association_id, options = {}, &extension) ⇒ Object



21
22
23
# File 'lib/mongo_mapper/plugins/associations.rb', line 21

def one(association_id, options={}, &extension)
  create_association(OneAssociation.new(association_id, options, &extension))
end