Module: MongoMapper::Plugins::Keys::InstanceMethods
- Defined in:
- lib/mongo_mapper/plugins/keys.rb
Instance Method Summary collapse
- #[](name) ⇒ Object
- #[]=(name, value) ⇒ Object
- #assign(attrs = {}) ⇒ Object
- #attributes ⇒ Object (also: #to_mongo)
- #attributes=(attrs) ⇒ Object
- #default_id_value(attrs = {}) ⇒ Object
- #embedded_keys ⇒ Object
- #id ⇒ Object
- #id=(value) ⇒ Object
- #initialize(attrs = {}) ⇒ Object
- #initialize_from_database(attrs = {}) ⇒ Object
- #key_names ⇒ Object
- #keys ⇒ Object
- #non_embedded_keys ⇒ Object
- #persisted? ⇒ Boolean
- #update_attribute(name, value) ⇒ Object
- #update_attributes(attrs = {}) ⇒ Object
- #update_attributes!(attrs = {}) ⇒ Object
Instance Method Details
#[](name) ⇒ Object
244 245 246 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 244 def [](name) read_key(name) end |
#[]=(name, value) ⇒ Object
248 249 250 251 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 248 def []=(name, value) ensure_key_exists(name) write_key(name, value) end |
#assign(attrs = {}) ⇒ Object
214 215 216 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 214 def assign(attrs={}) self.attributes = attrs end |
#attributes ⇒ Object Also known as: to_mongo
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 194 def attributes HashWithIndifferentAccess.new.tap do |attrs| keys.each_pair do |name, key| value = key.set(self[key.name]) attrs[name] = value end .each do |association| if documents = instance_variable_get(association.ivar) if association.is_a?(Associations::OneAssociation) attrs[association.name] = documents.to_mongo else attrs[association.name] = documents.map { |document| document.to_mongo } end end end end end |
#attributes=(attrs) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 182 def attributes=(attrs) return if attrs.blank? attrs.each_pair do |key, value| if respond_to?(:"#{key}=") self.send(:"#{key}=", value) else self[key] = value end end end |
#default_id_value(attrs = {}) ⇒ Object
269 270 271 272 273 274 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 269 def default_id_value(attrs={}) id_provided = !attrs.nil? && attrs.keys.map { |k| k.to_s }.detect { |k| k == 'id' || k == '_id' } if !id_provided && self.class.can_default_id? write_key :_id, BSON::ObjectId.new end end |
#embedded_keys ⇒ Object
265 266 267 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 265 def keys.values.select { |key| key. } end |
#id ⇒ Object
232 233 234 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 232 def id _id end |
#id=(value) ⇒ Object
236 237 238 239 240 241 242 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 236 def id=(value) if self.class.using_object_id? value = ObjectId.to_mongo(value) end self[:_id] = value end |
#initialize(attrs = {}) ⇒ Object
165 166 167 168 169 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 165 def initialize(attrs={}) default_id_value(attrs) @_new = true assign(attrs) end |
#initialize_from_database(attrs = {}) ⇒ Object
171 172 173 174 175 176 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 171 def initialize_from_database(attrs={}) @_new = false load_from_database(attrs) default_id_value(attrs) self end |
#key_names ⇒ Object
257 258 259 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 257 def key_names keys.keys end |
#keys ⇒ Object
253 254 255 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 253 def keys self.class.keys end |
#non_embedded_keys ⇒ Object
261 262 263 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 261 def keys.values.select { |key| !key. } end |
#persisted? ⇒ Boolean
178 179 180 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 178 def persisted? !new? && !destroyed? end |
#update_attribute(name, value) ⇒ Object
228 229 230 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 228 def update_attribute(name, value) update_attributes(name.to_sym => value) end |
#update_attributes(attrs = {}) ⇒ Object
218 219 220 221 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 218 def update_attributes(attrs={}) assign(attrs) save end |
#update_attributes!(attrs = {}) ⇒ Object
223 224 225 226 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 223 def update_attributes!(attrs={}) assign(attrs) save! end |