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.
10 11 12 13 14 15 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 10 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.
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 |
#query_options ⇒ Object (readonly)
Returns the value of attribute query_options.
5 6 7 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 5 def @query_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
66 67 68 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 66 def as @options[:as] || self.name end |
#as? ⇒ Boolean
50 51 52 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 50 def as? !!@options[:as] end |
#belongs_to? ⇒ Boolean
38 39 40 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 38 def belongs_to? @type == :belongs_to end |
#class_name ⇒ Object
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 = [:class_name] cn elsif many? name.to_s.singularize.camelize else name.to_s.camelize end end |
#embeddable? ⇒ Boolean
58 59 60 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 58 def many? && klass. end |
#foreign_key ⇒ Object
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
54 55 56 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 54 def in_array? !!@options[:in] end |
#ivar ⇒ Object
74 75 76 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 74 def ivar @ivar ||= "@_#{name}" end |
#klass ⇒ Object
30 31 32 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 30 def klass @klass ||= [:class] || class_name.constantize end |
#many? ⇒ Boolean
34 35 36 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 34 def many? @type == :many end |
#one? ⇒ Boolean
42 43 44 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 42 def one? @type == :one end |
#polymorphic? ⇒ Boolean
46 47 48 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 46 def polymorphic? !!@options[:polymorphic] end |
#proxy_class ⇒ Object
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. 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_name ⇒ Object
62 63 64 |
# File 'lib/mongo_mapper/plugins/associations/base.rb', line 62 def type_key_name many? ? '_type' : "#{as}_type" end |