Module: MongoMapper::Plugins::Keys::InstanceMethods

Defined in:
lib/mongo_mapper/plugins/keys.rb

Instance Method Summary collapse

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

#attributesObject 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

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

#embedded_keysObject



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

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

#idObject



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_namesObject



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

def key_names
  keys.keys
end

#keysObject



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

def keys
  self.class.keys
end

#non_embedded_keysObject



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

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

#persisted?Boolean

Returns:



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