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, :polymorphic, :autosave, :touch]

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



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

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?
  !!@options[:as]
end

#autosave?Boolean

Returns:

Raises:

  • (NotImplementedError)


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

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

#embeddable?Boolean

Returns:



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

def embeddable?
  klass.embeddable?
end

#foreign_keyObject



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

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

#ivarObject



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

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

#polymorphic?Boolean

Returns:



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

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

#proxy_classObject

Raises:

  • (NotImplementedError)


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

def proxy_class
  raise NotImplementedError
end

#setup(model) ⇒ Object



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

def setup(model)
end

#touch?Boolean

Returns:



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

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

#type_key_nameObject



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

def type_key_name
  "_type"
end