Class: MongoMapper::Plugins::Associations::Base
- 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
-
#finder_options ⇒ Object
readonly
Returns the value of attribute finder_options.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #as ⇒ Object
- #as? ⇒ Boolean
- #belongs_to? ⇒ Boolean
- #class_name ⇒ Object
- #embeddable? ⇒ Boolean
- #foreign_key ⇒ Object
- #in_array? ⇒ Boolean
-
#initialize(type, name, options = {}, &extension) ⇒ Base
constructor
A new instance of Base.
- #ivar ⇒ Object
- #klass ⇒ Object
- #many? ⇒ Boolean
- #one? ⇒ Boolean
- #polymorphic? ⇒ Boolean
- #proxy_class ⇒ Object
- #type_key_name ⇒ Object
Constructor Details
#initialize(type, name, options = {}, &extension) ⇒ Base
Returns a new instance of Base.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 10 def initialize(type, name, ={}, &extension) @type, @name, @options, @finder_options = type, name, {}, {} .symbolize_keys! [:extend] = modulized_extensions(extension, [:extend]) .each_pair do |key, value| if AssociationOptions.include?(key) @options[key] = value else @finder_options[key] = value end end end |
Instance Attribute Details
#finder_options ⇒ Object (readonly)
Returns the value of attribute finder_options.
5 6 7 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 5 def @finder_options end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 5 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 5 def @options end |
#type ⇒ Object (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
#as ⇒ Object
73 74 75 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 73 def as @options[:as] || self.name end |
#as? ⇒ Boolean
57 58 59 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 57 def as? !!@options[:as] end |
#belongs_to? ⇒ Boolean
45 46 47 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 45 def belongs_to? @belongs_to_type ||= @type == :belongs_to end |
#class_name ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 25 def class_name @class_name ||= begin if cn = [:class_name] cn elsif many? name.to_s.singularize.camelize else name.to_s.camelize end end end |
#embeddable? ⇒ Boolean
65 66 67 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 65 def many? && klass. end |
#foreign_key ⇒ Object
77 78 79 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 77 def foreign_key @options[:foreign_key] || "#{name}_id" end |
#in_array? ⇒ Boolean
61 62 63 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 61 def in_array? !!@options[:in] end |
#ivar ⇒ Object
81 82 83 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 81 def ivar @ivar ||= "@_#{name}" end |
#klass ⇒ Object
37 38 39 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 37 def klass @klass ||= [:class] || class_name.constantize end |
#many? ⇒ Boolean
41 42 43 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 41 def many? @many_type ||= @type == :many end |
#one? ⇒ Boolean
49 50 51 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 49 def one? @one_type ||= @type == :one end |
#polymorphic? ⇒ Boolean
53 54 55 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 53 def polymorphic? !!@options[:polymorphic] end |
#proxy_class ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 85 def proxy_class @proxy_class ||= begin if many? if self.klass. 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 end |
#type_key_name ⇒ Object
69 70 71 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 69 def type_key_name @type_key_name ||= many? ? '_type' : "#{as}_type" end |