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_attributes(attrs = {}) ⇒ Object
- #update_attributes!(attrs = {}) ⇒ Object
Instance Method Details
#[](name) ⇒ Object
236 237 238 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 236 def [](name) read_key(name) end |
#[]=(name, value) ⇒ Object
240 241 242 243 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 240 def []=(name, value) ensure_key_exists(name) write_key(name, value) end |
#assign(attrs = {}) ⇒ Object
210 211 212 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 210 def assign(attrs={}) self.attributes = attrs end |
#attributes ⇒ Object Also known as: to_mongo
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 190 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.one? attrs[association.name] = documents.to_mongo else attrs[association.name] = documents.map { |document| document.to_mongo } end end end end end |
#attributes=(attrs) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 178 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
261 262 263 264 265 266 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 261 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
257 258 259 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 257 def keys.values.select { |key| key. } end |
#id ⇒ Object
224 225 226 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 224 def id _id end |
#id=(value) ⇒ Object
228 229 230 231 232 233 234 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 228 def id=(value) if self.class.using_object_id? value = ObjectId.to_mongo(value) end self[:_id] = value end |
#initialize(attrs = {}) ⇒ Object
162 163 164 165 166 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 162 def initialize(attrs={}) default_id_value(attrs) @_new = true assign(attrs) end |
#initialize_from_database(attrs = {}) ⇒ Object
168 169 170 171 172 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 168 def initialize_from_database(attrs={}) @_new = false load_from_database(attrs) self end |
#key_names ⇒ Object
249 250 251 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 249 def key_names keys.keys end |
#keys ⇒ Object
245 246 247 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 245 def keys self.class.keys end |
#non_embedded_keys ⇒ Object
253 254 255 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 253 def keys.values.select { |key| !key. } end |
#persisted? ⇒ Boolean
174 175 176 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 174 def persisted? !new? && !destroyed? end |
#update_attributes(attrs = {}) ⇒ Object
214 215 216 217 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 214 def update_attributes(attrs={}) assign(attrs) save end |
#update_attributes!(attrs = {}) ⇒ Object
219 220 221 222 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 219 def update_attributes!(attrs={}) assign(attrs) save! end |