Module: MongoMapper::Plugins::Keys

Extended by:
ActiveSupport::Concern
Included in:
Document, EmbeddedDocument
Defined in:
lib/mongo_mapper/plugins/keys.rb,
lib/mongo_mapper/plugins/keys/key.rb

Defined Under Namespace

Modules: ClassMethods Classes: Key

Instance Method Summary collapse

Instance Method Details

#[](name) ⇒ Object



245
246
247
# File 'lib/mongo_mapper/plugins/keys.rb', line 245

def [](name)
  read_key(name)
end

#[]=(name, value) ⇒ Object



249
250
251
252
# File 'lib/mongo_mapper/plugins/keys.rb', line 249

def []=(name, value)
  ensure_key_exists(name)
  write_key(name, value)
end

#assign(attrs = {}) ⇒ Object



213
214
215
216
# File 'lib/mongo_mapper/plugins/keys.rb', line 213

def assign(attrs={})
  warn "[DEPRECATION] #assign is deprecated, use #attributes="
  self.attributes = attrs
end

#attributesObject Also known as: to_mongo



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/mongo_mapper/plugins/keys.rb', line 193

def attributes
  HashWithIndifferentAccess.new.tap do |attrs|
    keys.select { |name,key| !self[key.name].nil? || key.type == ObjectId }.each do |name, key|
      value = key.set(self[key.name])
      attrs[name] = value
    end

    embedded_associations.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



181
182
183
184
185
186
187
188
189
190
191
# File 'lib/mongo_mapper/plugins/keys.rb', line 181

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

#embedded_keysObject



266
267
268
# File 'lib/mongo_mapper/plugins/keys.rb', line 266

def embedded_keys
  keys.values.select { |key| key.embeddable? }
end

#idObject



233
234
235
# File 'lib/mongo_mapper/plugins/keys.rb', line 233

def id
  _id
end

#id=(value) ⇒ Object



237
238
239
240
241
242
243
# File 'lib/mongo_mapper/plugins/keys.rb', line 237

def id=(value)
  if self.class.using_object_id?
    value = ObjectId.to_mongo(value)
  end

  self[:_id] = value
end

#initialize(attrs = {}) ⇒ Object



166
167
168
169
# File 'lib/mongo_mapper/plugins/keys.rb', line 166

def initialize(attrs={})
  @_new = true
  self.attributes = attrs
end

#initialize_from_database(attrs = {}) ⇒ Object



171
172
173
174
175
# File 'lib/mongo_mapper/plugins/keys.rb', line 171

def initialize_from_database(attrs={})
  @_new = false
  load_from_database(attrs)
  self
end

#key_namesObject



258
259
260
# File 'lib/mongo_mapper/plugins/keys.rb', line 258

def key_names
  keys.keys
end

#keysObject



254
255
256
# File 'lib/mongo_mapper/plugins/keys.rb', line 254

def keys
  self.class.keys
end

#non_embedded_keysObject



262
263
264
# File 'lib/mongo_mapper/plugins/keys.rb', line 262

def non_embedded_keys
  keys.values.select { |key| !key.embeddable? }
end

#persisted?Boolean

Returns:



177
178
179
# File 'lib/mongo_mapper/plugins/keys.rb', line 177

def persisted?
  !new? && !destroyed?
end

#update_attribute(name, value) ⇒ Object



228
229
230
231
# File 'lib/mongo_mapper/plugins/keys.rb', line 228

def update_attribute(name, value)
  self.send(:"#{name}=", value)
  save(:validate => false)
end

#update_attributes(attrs = {}) ⇒ Object



218
219
220
221
# File 'lib/mongo_mapper/plugins/keys.rb', line 218

def update_attributes(attrs={})
  self.attributes = attrs
  save
end

#update_attributes!(attrs = {}) ⇒ Object



223
224
225
226
# File 'lib/mongo_mapper/plugins/keys.rb', line 223

def update_attributes!(attrs={})
  self.attributes = attrs
  save!
end