Class: MongoMapper::Associations::Base

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

Overview

Base class for keeping track of associations.

Constant Summary collapse

AssociationOptions =

Options that should not be considered MongoDB query options/criteria

[:as, :class, :class_name, :dependent, :extend, :foreign_key, :polymorphic]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Base.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mongo_mapper/associations/base.rb', line 12

def initialize(type, name, options={}, &extension)
  @type, @name, @options, @finder_options = type, name, {}, {}
  options.symbolize_keys!

  options[:extend] = modulized_extensions(extension, options[:extend])

  options.each_pair do |key, value|
    if AssociationOptions.include?(key)
      @options[key] = value
    else
      @finder_options[key] = value
    end
  end
end

Instance Attribute Details

#finder_optionsObject (readonly)

Returns the value of attribute finder_options.



7
8
9
# File 'lib/mongo_mapper/associations/base.rb', line 7

def finder_options
  @finder_options
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/mongo_mapper/associations/base.rb', line 7

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/mongo_mapper/associations/base.rb', line 7

def options
  @options
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/mongo_mapper/associations/base.rb', line 7

def type
  @type
end

Instance Method Details

#asObject



63
64
65
# File 'lib/mongo_mapper/associations/base.rb', line 63

def as
  @options[:as] || self.name
end

#as?Boolean

Returns:



55
56
57
# File 'lib/mongo_mapper/associations/base.rb', line 55

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

#belongs_to?Boolean

Returns:



47
48
49
# File 'lib/mongo_mapper/associations/base.rb', line 47

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

#class_nameObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mongo_mapper/associations/base.rb', line 27

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:



75
76
77
# File 'lib/mongo_mapper/associations/base.rb', line 75

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

#foreign_keyObject



67
68
69
# File 'lib/mongo_mapper/associations/base.rb', line 67

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

#ivarObject



71
72
73
# File 'lib/mongo_mapper/associations/base.rb', line 71

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

#klassObject



39
40
41
# File 'lib/mongo_mapper/associations/base.rb', line 39

def klass
  @klass ||= options[:class] || class_name.constantize
end

#many?Boolean

Returns:



43
44
45
# File 'lib/mongo_mapper/associations/base.rb', line 43

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

#polymorphic?Boolean

Returns:



51
52
53
# File 'lib/mongo_mapper/associations/base.rb', line 51

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

#proxy_classObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/mongo_mapper/associations/base.rb', line 79

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

#type_key_nameObject



59
60
61
# File 'lib/mongo_mapper/associations/base.rb', line 59

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