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

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

Direct Known Subclasses

ManyAssociation, SingleAssociation

Constant Summary collapse

AssociationOptions =

Options that should not be considered MongoDB query options/criteria

[:as, :class, :class_name, :dependent, :extend, :foreign_key, :in, :from, :polymorphic, :autosave, :touch, :counter_cache, :ordered]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Base.



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

def initialize(name, options={}, &extension)
  @name, @options, @query_options, @original_options = name.to_sym, {}, {}, 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.



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

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#query_optionsObject (readonly)

Returns the value of attribute query_options.



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

def query_options
  @query_options
end

Instance Method Details

#asObject



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

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

#as?Boolean

Returns:



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

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

#autosave?Boolean

Returns:

Raises:

  • (NotImplementedError)


79
80
81
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 79

def autosave?
  raise NotImplementedError
end

#class_nameObject



18
19
20
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 18

def class_name
  @class_name ||= options[:class_name] || name.to_s.camelize
end

#counter_cache?Boolean

Returns:



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

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

#embeddable?Boolean

Returns:



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

def embeddable?
  klass.embeddable?
end

#foreign_keyObject



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

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

#in_array?Boolean

Returns:



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

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

#in_foreign_array?Boolean

Returns:



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

def in_foreign_array?
  !!@options[:from]
end

#ivarObject



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

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

#klassObject



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

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

#ordered?Boolean

Returns:



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

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

#polymorphic?Boolean

Returns:



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

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

#proxy_classObject

:nocov:

Raises:

  • (NotImplementedError)


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

def proxy_class
  raise NotImplementedError
end

#setup(model) ⇒ Object

:nocov:



84
85
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 84

def setup(model)
end

#touch?Boolean

Returns:



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

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

#type_key_nameObject



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

def type_key_name
  "_type"
end