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
# File 'lib/mongomapper/associations/base.rb', line 6

def initialize(type, name, options = {})
  @type, @name, @options = type, name, options
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

#belongs_to?Boolean

Returns:



30
31
32
# File 'lib/mongomapper/associations/base.rb', line 30

def belongs_to?
  @belongs_to_type ||= @type == :belongs_to
end

#class_nameObject



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/mongomapper/associations/base.rb', line 10

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

#embeddable?Boolean

Returns:



50
51
52
# File 'lib/mongomapper/associations/base.rb', line 50

def embeddable?
  many? && klass.embeddable?
end

#foreign_keyObject



42
43
44
# File 'lib/mongomapper/associations/base.rb', line 42

def foreign_key
  @options[:foreign_key] || "#{name}_id"
end

#ivarObject



46
47
48
# File 'lib/mongomapper/associations/base.rb', line 46

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

#klassObject



22
23
24
# File 'lib/mongomapper/associations/base.rb', line 22

def klass
  @klass ||= class_name.constantize
end

#many?Boolean

Returns:



26
27
28
# File 'lib/mongomapper/associations/base.rb', line 26

def many?
  @many_type ||= @type == :many
end

#polymorphic?Boolean

Returns:



34
35
36
# File 'lib/mongomapper/associations/base.rb', line 34

def polymorphic?
  !!@options[:polymorphic]
end

#proxy_classObject



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/mongomapper/associations/base.rb', line 54

def proxy_class
  @proxy_class ||= begin
    if many?
      if self.klass.embeddable?
        polymorphic? ? ManyEmbeddedPolymorphicProxy : ManyEmbeddedProxy
      else
        polymorphic? ? ManyPolymorphicProxy : ManyProxy
      end
    else
      polymorphic? ? BelongsToPolymorphicProxy : BelongsToProxy
    end
  end # end begin
end

#type_key_nameObject



38
39
40
# File 'lib/mongomapper/associations/base.rb', line 38

def type_key_name
  @type_key_name ||= many? ? '_type' : "#{name}_type"
end