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
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#query_options ⇒ Object
readonly
Returns the value of attribute query_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
hate this, need to revisit.
- #type_key_name ⇒ Object
Constructor Details
#initialize(type, 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(type, name, ={}, &extension) @type, @name, @options, @query_options, @original_options = type, name, {}, {}, .symbolize_keys! [:extend] = modularized_extensions(extension, [:extend]) end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 6 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 6 def @options end |
#query_options ⇒ Object (readonly)
Returns the value of attribute query_options.
6 7 8 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 6 def @query_options end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
6 7 8 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 6 def type @type end |
Instance Method Details
#as ⇒ Object
67 68 69 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 67 def as @options[:as] || self.name end |
#as? ⇒ Boolean
51 52 53 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 51 def as? !!@options[:as] end |
#belongs_to? ⇒ Boolean
39 40 41 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 39 def belongs_to? @type == :belongs_to end |
#class_name ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 18 def class_name return @class_name if defined?(@class_name) @class_name = if cn = [:class_name] cn elsif many? name.to_s.singularize.camelize else name.to_s.camelize end end |
#embeddable? ⇒ Boolean
59 60 61 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 59 def (one? || many?) && klass. end |
#foreign_key ⇒ Object
71 72 73 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 71 def foreign_key @options[:foreign_key] || "#{name}_id" end |
#in_array? ⇒ Boolean
55 56 57 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 55 def in_array? !!@options[:in] end |
#ivar ⇒ Object
75 76 77 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 75 def ivar @ivar ||= "@_#{name}" end |
#klass ⇒ Object
31 32 33 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 31 def klass @klass ||= [:class] || class_name.constantize end |
#many? ⇒ Boolean
35 36 37 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 35 def many? @type == :many end |
#one? ⇒ Boolean
43 44 45 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 43 def one? @type == :one end |
#polymorphic? ⇒ Boolean
47 48 49 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 47 def polymorphic? !!@options[:polymorphic] end |
#proxy_class ⇒ Object
hate this, need to revisit
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 80 def proxy_class return @proxy_class if defined?(@proxy_class) @proxy_class = if many? if klass. polymorphic? ? ManyEmbeddedPolymorphicProxy : ManyEmbeddedProxy else if polymorphic? ManyPolymorphicProxy elsif as? ManyDocumentsAsProxy elsif in_array? InArrayProxy else ManyDocumentsProxy end end elsif one? klass. ? OneEmbeddedProxy : OneProxy else polymorphic? ? BelongsToPolymorphicProxy : BelongsToProxy end end |
#type_key_name ⇒ Object
63 64 65 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 63 def type_key_name many? ? '_type' : "#{as}_type" end |