Class: MongoMapper::Plugins::Associations::SingleAssociation

Inherits:
Base
  • Object
show all
Defined in:
lib/mongo_mapper/plugins/associations/single_association.rb

Direct Known Subclasses

BelongsToAssociation, OneAssociation

Constant Summary

Constants inherited from Base

Base::AssociationOptions

Instance Attribute Summary

Attributes inherited from Base

#name, #options, #query_options

Instance Method Summary collapse

Methods inherited from Base

#as, #as?, #autosave?, #class_name, #counter_cache?, #embeddable?, #foreign_key, #in_array?, #in_foreign_array?, #initialize, #ivar, #klass, #ordered?, #polymorphic?, #proxy_class, #touch?, #type_key_name

Constructor Details

This class inherits a constructor from MongoMapper::Plugins::Associations::Base

Instance Method Details

#setup(model) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/mongo_mapper/plugins/associations/single_association.rb', line 6

def setup(model)
  @model = model

  model.associations_module.module_eval(<<-end_eval, __FILE__, __LINE__ + 1)
    def #{name}
      get_proxy(associations[#{name.inspect}]).read
    end

    def #{name}=(value)
      get_proxy(associations[#{name.inspect}]).write(value)
    end

    def #{name}?
      get_proxy(associations[#{name.inspect}]).present?
    end

    def build_#{name}(attrs={}, &block)
      get_proxy(associations[#{name.inspect}]).build(attrs, &block)
    end

    def create_#{name}(attrs={}, &block)
      get_proxy(associations[#{name.inspect}]).create(attrs, &block)
    end

    def create_#{name}!(attrs={}, &block)
      get_proxy(associations[#{name.inspect}]).create!(attrs, &block)
    end
  end_eval
end