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
- #embedded_keys ⇒ Object
- #id ⇒ Object
- #id=(value) ⇒ Object
- #key_names ⇒ Object
- #keys ⇒ Object
- #new? ⇒ Boolean
- #non_embedded_keys ⇒ Object
- #update_attributes(attrs = {}) ⇒ Object
- #update_attributes!(attrs = {}) ⇒ Object
Instance Method Details
#[](name) ⇒ Object
215 216 217 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 215 def [](name) read_key(name) end |
#[]=(name, value) ⇒ Object
219 220 221 222 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 219 def []=(name, value) ensure_key_exists(name) write_key(name, value) end |
#assign(attrs = {}) ⇒ Object
189 190 191 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 189 def assign(attrs={}) self.attributes = attrs end |
#attributes ⇒ Object Also known as: to_mongo
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 171 def attributes attrs = HashWithIndifferentAccess.new 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) attrs[association.name] = documents.map { |document| document.to_mongo } end end attrs end |
#attributes=(attrs) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 157 def attributes=(attrs) return if attrs.blank? attrs.each_pair do |name, value| writer_method = "#{name}=" if respond_to?(writer_method) self.send(writer_method, value) else self[name.to_s] = value end end end |
#embedded_keys ⇒ Object
240 241 242 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 240 def keys.values.select { |key| key. } end |
#id ⇒ Object
203 204 205 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 203 def id _id end |
#id=(value) ⇒ Object
207 208 209 210 211 212 213 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 207 def id=(value) if self.class.using_object_id? value = MongoMapper.normalize_object_id(value) end self[:_id] = value end |
#key_names ⇒ Object
230 231 232 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 230 def key_names keys.keys end |
#keys ⇒ Object
225 226 227 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 225 def keys self.class.keys end |
#non_embedded_keys ⇒ Object
235 236 237 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 235 def keys.values.select { |key| !key. } end |
#update_attributes(attrs = {}) ⇒ Object
193 194 195 196 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 193 def update_attributes(attrs={}) assign(attrs) save end |
#update_attributes!(attrs = {}) ⇒ Object
198 199 200 201 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 198 def update_attributes!(attrs={}) assign(attrs) save! end |