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
- #initialize(attrs = {}, from_database = false) ⇒ 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
229 230 231 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 229 def [](name) read_key(name) end |
#[]=(name, value) ⇒ Object
233 234 235 236 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 233 def []=(name, value) ensure_key_exists(name) write_key(name, value) end |
#assign(attrs = {}) ⇒ Object
203 204 205 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 203 def assign(attrs={}) self.attributes = attrs end |
#attributes ⇒ Object Also known as: to_mongo
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 185 def attributes attrs = defined?(HashWithIndifferentAccess) ? HashWithIndifferentAccess.new : ActiveSupport::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
171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 171 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
254 255 256 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 254 def keys.values.select { |key| key. } end |
#id ⇒ Object
217 218 219 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 217 def id _id end |
#id=(value) ⇒ Object
221 222 223 224 225 226 227 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 221 def id=(value) if self.class.using_object_id? value = MongoMapper.normalize_object_id(value) end self[:_id] = value end |
#initialize(attrs = {}, from_database = false) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 148 def initialize(attrs={}, from_database=false) unless attrs.nil? provided_keys = attrs.keys.map { |k| k.to_s } unless provided_keys.include?('_id') || provided_keys.include?('id') write_key :_id, Mongo::ObjectID.new end end assign_type_if_present if from_database @new = false self.attributes = attrs else @new = true assign(attrs) end end |
#key_names ⇒ Object
244 245 246 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 244 def key_names keys.keys end |
#keys ⇒ Object
239 240 241 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 239 def keys self.class.keys end |
#non_embedded_keys ⇒ Object
249 250 251 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 249 def keys.values.select { |key| !key. } end |
#update_attributes(attrs = {}) ⇒ Object
207 208 209 210 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 207 def update_attributes(attrs={}) assign(attrs) save end |
#update_attributes!(attrs = {}) ⇒ Object
212 213 214 215 |
# File 'lib/mongo_mapper/plugins/keys.rb', line 212 def update_attributes!(attrs={}) assign(attrs) save! end |