Class: MongoMapper::Plugins::Associations::Base

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

Constant Summary collapse

AssociationOptions =

Options that should not be considered MongoDB query options/criteria

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Base.



10
11
12
13
14
15
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 10

def initialize(type, name, options={}, &extension)
  @type, @name, @options, @query_options, @original_options = type, name, {}, {}, options
  options.symbolize_keys!
  options[:extend] = modularized_extensions(extension, options[:extend])
  separate_options_and_conditions
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 5

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 5

def options
  @options
end

#query_optionsObject (readonly)

Returns the value of attribute query_options.



5
6
7
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 5

def query_options
  @query_options
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 5

def type
  @type
end

Instance Method Details

#asObject



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

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

#as?Boolean

Returns:



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

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

#belongs_to?Boolean

Returns:



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

def belongs_to?
  @type == :belongs_to
end

#class_nameObject



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 17

def class_name
  return @class_name if defined?(@class_name)
  
  @class_name = 
    if cn = options[:class_name]
      cn
    elsif many?
      name.to_s.singularize.camelize
    else
      name.to_s.camelize
    end
end

#embeddable?Boolean

Returns:



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

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

#foreign_keyObject



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

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

#in_array?Boolean

Returns:



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

def in_array?
  !!@options[:in]
end

#ivarObject



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

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

#klassObject



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

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

#many?Boolean

Returns:



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

def many?
  @type == :many
end

#one?Boolean

Returns:



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

def one?
  @type == :one
end

#polymorphic?Boolean

Returns:



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

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

#proxy_classObject

hate this, need to revisit



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

def proxy_class
  return @proxy_class if defined?(@proxy_class)
  
  @proxy_class = 
    if many?
      if klass.embeddable?
        polymorphic? ? ManyEmbeddedPolymorphicProxy : ManyEmbeddedProxy
      else
        if polymorphic?
          ManyPolymorphicProxy
        elsif as?
          ManyDocumentsAsProxy
        elsif in_array?
          InArrayProxy
        else
          ManyDocumentsProxy
        end
      end
    elsif one?
      OneProxy
    else
      polymorphic? ? BelongsToPolymorphicProxy : BelongsToProxy
    end
end

#type_key_nameObject



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

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