Class: MongoMapper::Associations::Base
- Defined in:
- lib/mongomapper/associations/base.rb
Instance Attribute Summary collapse
-
#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
-
#initialize(type, name, options = {}) ⇒ Base
constructor
A new instance of Base.
- #ivar ⇒ Object
- #klass ⇒ Object
- #many? ⇒ Boolean
- #polymorphic? ⇒ Boolean
- #proxy_class ⇒ Object
- #type_key_name ⇒ Object
Constructor Details
#initialize(type, name, options = {}) ⇒ Base
Returns a new instance of Base.
6 7 8 |
# File 'lib/mongomapper/associations/base.rb', line 6 def initialize(type, name, = {}) @type, @name, @options = type, name, end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/mongomapper/associations/base.rb', line 4 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'lib/mongomapper/associations/base.rb', line 4 def @options end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
4 5 6 |
# File 'lib/mongomapper/associations/base.rb', line 4 def type @type end |
Instance Method Details
#as ⇒ Object
46 47 48 |
# File 'lib/mongomapper/associations/base.rb', line 46 def as @options[:as] || self.name end |
#as? ⇒ Boolean
38 39 40 |
# File 'lib/mongomapper/associations/base.rb', line 38 def as? !!@options[:as] end |
#belongs_to? ⇒ Boolean
30 31 32 |
# File 'lib/mongomapper/associations/base.rb', line 30 def belongs_to? @belongs_to_type ||= @type == :belongs_to end |
#class_name ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/mongomapper/associations/base.rb', line 10 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
58 59 60 |
# File 'lib/mongomapper/associations/base.rb', line 58 def many? && klass. end |
#foreign_key ⇒ Object
50 51 52 |
# File 'lib/mongomapper/associations/base.rb', line 50 def foreign_key @options[:foreign_key] || "#{name}_id" end |
#ivar ⇒ Object
54 55 56 |
# File 'lib/mongomapper/associations/base.rb', line 54 def ivar @ivar ||= "@_#{name}" end |
#klass ⇒ Object
22 23 24 |
# File 'lib/mongomapper/associations/base.rb', line 22 def klass @klass ||= class_name.constantize end |
#many? ⇒ Boolean
26 27 28 |
# File 'lib/mongomapper/associations/base.rb', line 26 def many? @many_type ||= @type == :many end |
#polymorphic? ⇒ Boolean
34 35 36 |
# File 'lib/mongomapper/associations/base.rb', line 34 def polymorphic? !!@options[:polymorphic] end |
#proxy_class ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/mongomapper/associations/base.rb', line 62 def proxy_class @proxy_class ||= begin if many? if self.klass. polymorphic? ? ManyEmbeddedPolymorphicProxy : ManyEmbeddedProxy else if polymorphic? ManyPolymorphicProxy elsif as? ManyDocumentsAsProxy else ManyProxy end end else polymorphic? ? BelongsToPolymorphicProxy : BelongsToProxy end end # end begin end |
#type_key_name ⇒ Object
42 43 44 |
# File 'lib/mongomapper/associations/base.rb', line 42 def type_key_name @type_key_name ||= many? ? '_type' : "#{as}_type" end |