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

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

Instance Method Summary collapse

Instance Method Details

#[](name) ⇒ Object



237
238
239
# File 'lib/mongo_mapper/plugins/keys.rb', line 237

def [](name)
  read_key(name)
end

#[]=(name, value) ⇒ Object



241
242
243
244
# File 'lib/mongo_mapper/plugins/keys.rb', line 241

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

#assign(attrs = {}) ⇒ Object



211
212
213
# File 'lib/mongo_mapper/plugins/keys.rb', line 211

def assign(attrs={})
  self.attributes = attrs
end

#attributesObject Also known as: to_mongo



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

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



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

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



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

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

#idObject



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

def id
  _id
end

#id=(value) ⇒ Object



229
230
231
232
233
234
235
# File 'lib/mongo_mapper/plugins/keys.rb', line 229

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

  self[:_id] = value
end

#initialize(attrs = {}, from_database = false) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/mongo_mapper/plugins/keys.rb', line 163

def initialize(attrs={}, from_database=false)
  default_id_value(attrs)

  if from_database
    @_new = false
    load_from_database(attrs)
  else
    @_new = true
    assign(attrs)
  end
end

#key_namesObject



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

def key_names
  keys.keys
end

#keysObject



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

def keys
  self.class.keys
end

#non_embedded_keysObject



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

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

#persisted?Boolean

Returns:



175
176
177
# File 'lib/mongo_mapper/plugins/keys.rb', line 175

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

#update_attributes(attrs = {}) ⇒ Object



215
216
217
218
# File 'lib/mongo_mapper/plugins/keys.rb', line 215

def update_attributes(attrs={})
  assign(attrs)
  save
end

#update_attributes!(attrs = {}) ⇒ Object



220
221
222
223
# File 'lib/mongo_mapper/plugins/keys.rb', line 220

def update_attributes!(attrs={})
  assign(attrs)
  save!
end