Class: ActiveMongoid::Associations::Metadata
- Inherits:
-
Hash
- Object
- Hash
- ActiveMongoid::Associations::Metadata
- Defined in:
- lib/active_mongoid/associations/metadata.rb
Instance Method Summary collapse
- #as ⇒ Object
- #as? ⇒ Boolean
- #autosave ⇒ Object
- #autosave? ⇒ Boolean
- #builder(base, object) ⇒ Object
- #class_name ⇒ Object
- #criteria(object, type = nil) ⇒ Object
- #dependent ⇒ Object
- #destructive? ⇒ Boolean
- #determine_foreign_key ⇒ Object
- #determine_inverse_for(field) ⇒ Object
- #determine_inverse_relation ⇒ Object
- #determine_inverses ⇒ Object
- #determine_key ⇒ Object
- #foreign_key ⇒ Object
- #foreign_key_match ⇒ Object
- #foreign_key_setter ⇒ Object
-
#initialize(properties = {}) ⇒ Metadata
constructor
A new instance of Metadata.
- #inverse(other = nil) ⇒ Object
- #inverse_class_name ⇒ Object
- #inverse_klass ⇒ Object
- #inverse_metadata(object = nil) ⇒ Object
- #inverse_of ⇒ Object
- #inverse_relation ⇒ Object
- #inverse_setter(other = nil) ⇒ Object
- #inverse_type ⇒ Object
- #inverse_type_setter ⇒ Object
- #inverses(other = nil) ⇒ Object
- #key ⇒ Object
- #lookup_inverses(other) ⇒ Object
- #name ⇒ Object
- #object_class ⇒ Object (also: #klass)
- #object_name ⇒ Object
- #order ⇒ Object
- #plural_anme ⇒ Object
- #polymorphic? ⇒ Boolean
- #primary_key ⇒ Object
- #relation ⇒ Object
- #relations_metadata ⇒ Object
- #type ⇒ Object
- #type_setter ⇒ Object
Constructor Details
#initialize(properties = {}) ⇒ Metadata
Returns a new instance of Metadata.
15 16 17 |
# File 'lib/active_mongoid/associations/metadata.rb', line 15 def initialize(properties = {}) merge!(properties) end |
Instance Method Details
#as ⇒ Object
7 8 9 |
# File 'lib/active_mongoid/associations/metadata.rb', line 7 def as self[:as] end |
#as? ⇒ Boolean
11 12 13 |
# File 'lib/active_mongoid/associations/metadata.rb', line 11 def as? !!as end |
#autosave ⇒ Object
19 20 21 |
# File 'lib/active_mongoid/associations/metadata.rb', line 19 def autosave self[:autosave] end |
#autosave? ⇒ Boolean
23 24 25 |
# File 'lib/active_mongoid/associations/metadata.rb', line 23 def autosave? !!autosave end |
#builder(base, object) ⇒ Object
27 28 29 |
# File 'lib/active_mongoid/associations/metadata.rb', line 27 def builder(base, object) relation.builder(base, self, object) end |
#class_name ⇒ Object
82 83 84 |
# File 'lib/active_mongoid/associations/metadata.rb', line 82 def class_name (self[:class_name] || name.to_s.singularize.titleize.delete(' ')).sub(/\A::/,"") end |
#criteria(object, type = nil) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/active_mongoid/associations/metadata.rb', line 35 def criteria(object, type = nil) query = relation.criteria(self, object, type) if order query = query.respond_to?(:order_by) ? query.order_by(order) : query.order(order) end query end |
#dependent ⇒ Object
195 196 197 |
# File 'lib/active_mongoid/associations/metadata.rb', line 195 def dependent self[:dependent] end |
#destructive? ⇒ Boolean
199 200 201 |
# File 'lib/active_mongoid/associations/metadata.rb', line 199 def destructive? @destructive ||= (dependent == :delete || dependent == :destroy) end |
#determine_foreign_key ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/active_mongoid/associations/metadata.rb', line 67 def determine_foreign_key return self[:foreign_key].to_s if self[:foreign_key] if relation.stores_foreign_key? relation.foreign_key(name) else .foreign_key end end |
#determine_inverse_for(field) ⇒ Object
135 136 137 |
# File 'lib/active_mongoid/associations/metadata.rb', line 135 def determine_inverse_for(field) relation.stores_foreign_key? && polymorphic? ? "#{name}_#{field}" : nil end |
#determine_inverse_relation ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/active_mongoid/associations/metadata.rb', line 171 def determine_inverse_relation default = foreign_key_match || klass.am_relations[inverse_klass.name.underscore] return default.name if default klass.am_relations.each_pair do |key, | next if .name == name if .class_name == inverse_class_name return key.to_sym end end return nil end |
#determine_inverses ⇒ Object
161 162 163 164 165 |
# File 'lib/active_mongoid/associations/metadata.rb', line 161 def determine_inverses return [ inverse_of ] if has_key?(:inverse_of) return [ as ] if has_key?(:as) [ inverse_relation ] end |
#determine_key ⇒ Object
59 60 61 |
# File 'lib/active_mongoid/associations/metadata.rb', line 59 def determine_key relation.stores_foreign_key? ? foreign_key : primary_key end |
#foreign_key ⇒ Object
47 48 49 |
# File 'lib/active_mongoid/associations/metadata.rb', line 47 def foreign_key @foreign_key ||= determine_foreign_key end |
#foreign_key_match ⇒ Object
183 184 185 186 187 188 189 |
# File 'lib/active_mongoid/associations/metadata.rb', line 183 def foreign_key_match if fk = self[:foreign_key] .detect do || fk == .foreign_key if .stores_foreign_key? end end end |
#foreign_key_setter ⇒ Object
51 52 53 |
# File 'lib/active_mongoid/associations/metadata.rb', line 51 def foreign_key_setter @foreign_key_setter ||= "#{foreign_key}=" end |
#inverse(other = nil) ⇒ Object
143 144 145 146 |
# File 'lib/active_mongoid/associations/metadata.rb', line 143 def inverse(other = nil) invs = inverses(other) invs.first if invs.count == 1 end |
#inverse_class_name ⇒ Object
106 107 108 |
# File 'lib/active_mongoid/associations/metadata.rb', line 106 def inverse_class_name self[:as] || self[:inverse_class_name] end |
#inverse_klass ⇒ Object
102 103 104 |
# File 'lib/active_mongoid/associations/metadata.rb', line 102 def inverse_klass @inverse_klass ||= inverse_class_name.constantize if inverse_class_name end |
#inverse_metadata(object = nil) ⇒ Object
156 157 158 159 |
# File 'lib/active_mongoid/associations/metadata.rb', line 156 def (object = nil) object = object || object_class object.reflect_on_am_association(inverse(object)) end |
#inverse_of ⇒ Object
148 149 150 |
# File 'lib/active_mongoid/associations/metadata.rb', line 148 def inverse_of self[:inverse_of] end |
#inverse_relation ⇒ Object
167 168 169 |
# File 'lib/active_mongoid/associations/metadata.rb', line 167 def inverse_relation @inverse_relation ||= determine_inverse_relation end |
#inverse_setter(other = nil) ⇒ Object
152 153 154 |
# File 'lib/active_mongoid/associations/metadata.rb', line 152 def inverse_setter(other = nil) "#{inverse(other)}=" end |
#inverse_type ⇒ Object
139 140 141 |
# File 'lib/active_mongoid/associations/metadata.rb', line 139 def inverse_type @inverse_type ||= determine_inverse_for(:type) end |
#inverse_type_setter ⇒ Object
223 224 225 |
# File 'lib/active_mongoid/associations/metadata.rb', line 223 def inverse_type_setter @inverse_type_setter ||= "#{inverse_type}=" end |
#inverses(other = nil) ⇒ Object
110 111 112 113 114 115 116 |
# File 'lib/active_mongoid/associations/metadata.rb', line 110 def inverses(other = nil) if self[:polymorphic] lookup_inverses(other) else @inverses ||= determine_inverses end end |
#key ⇒ Object
43 44 45 |
# File 'lib/active_mongoid/associations/metadata.rb', line 43 def key relation.stores_foreign_key? ? foreign_key : primary_key end |
#lookup_inverses(other) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/active_mongoid/associations/metadata.rb', line 122 def lookup_inverses(other) return [ inverse_of ] if inverse_of if other matches = [] other.class.am_relations.values.each do || if .as.to_s == name && .class_name == inverse_class_name matches.push(.name) end end matches end end |
#name ⇒ Object
86 87 88 |
# File 'lib/active_mongoid/associations/metadata.rb', line 86 def name self[:name].to_s end |
#object_class ⇒ Object Also known as: klass
77 78 79 |
# File 'lib/active_mongoid/associations/metadata.rb', line 77 def object_class class_name.constantize end |
#object_name ⇒ Object
90 91 92 |
# File 'lib/active_mongoid/associations/metadata.rb', line 90 def object_name name.to_s.singularize end |
#order ⇒ Object
31 32 33 |
# File 'lib/active_mongoid/associations/metadata.rb', line 31 def order self[:order] end |
#plural_anme ⇒ Object
94 95 96 |
# File 'lib/active_mongoid/associations/metadata.rb', line 94 def plural_anme name.to_s.pluralize end |
#polymorphic? ⇒ Boolean
118 119 120 |
# File 'lib/active_mongoid/associations/metadata.rb', line 118 def polymorphic? @polymorphic ||= (!!self[:as] || !!self[:polymorphic]) end |
#primary_key ⇒ Object
63 64 65 |
# File 'lib/active_mongoid/associations/metadata.rb', line 63 def primary_key @primary_key ||= (self[:primary_key] || primary_key_default) end |
#relation ⇒ Object
98 99 100 |
# File 'lib/active_mongoid/associations/metadata.rb', line 98 def relation self[:relation] end |
#relations_metadata ⇒ Object
191 192 193 |
# File 'lib/active_mongoid/associations/metadata.rb', line 191 def klass.am_relations.values end |
#type ⇒ Object
207 208 209 |
# File 'lib/active_mongoid/associations/metadata.rb', line 207 def type @type ||= polymorphic? ? "#{as}_type" : nil end |
#type_setter ⇒ Object
55 56 57 |
# File 'lib/active_mongoid/associations/metadata.rb', line 55 def type_setter @type_setter ||= "#{type}=" end |