Class: MongoMapper::Associations::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/mongomapper/associations/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, name, options = {}) ⇒ Base

Returns a new instance of Base.



6
7
8
9
10
# File 'lib/mongomapper/associations/base.rb', line 6

def initialize(type, name, options = {})
  @options = options
  @type = type
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/mongomapper/associations/base.rb', line 4

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/mongomapper/associations/base.rb', line 4

def options
  @options
end

#typeObject (readonly)

Returns the value of attribute type.



4
5
6
# File 'lib/mongomapper/associations/base.rb', line 4

def type
  @type
end

Instance Method Details

#class_nameObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mongomapper/associations/base.rb', line 16

def class_name
  @class_name ||= begin
    if cn = options[:class_name]
      cn
    elsif @type == :many
      name.to_s.singularize.camelize
    else
      name.to_s.camelize
    end
  end
end

#ivarObject



28
29
30
# File 'lib/mongomapper/associations/base.rb', line 28

def ivar
  @ivar ||= "@_#{name}"
end

#klassObject



12
13
14
# File 'lib/mongomapper/associations/base.rb', line 12

def klass
  class_name.constantize
end

#proxy_classObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mongomapper/associations/base.rb', line 32

def proxy_class
  case @type
    when :belongs_to
      if @options[:polymorphic]
        PolymorphicBelongsToProxy
      else
        BelongsToProxy
      end
    when :many
      if self.klass.embeddable?
        if @options[:polymorphic]
          PolymorphicHasManyEmbeddedProxy
        else
          HasManyEmbeddedProxy
        end
      else
        HasManyProxy
      end
  end
end