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

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

Instance Method Summary collapse

Instance Method Details

#[](name) ⇒ Object



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

def [](name)
  read_key(name)
end

#[]=(name, value) ⇒ Object



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

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

#assign(attrs = {}) ⇒ Object



208
209
210
# File 'lib/mongo_mapper/plugins/keys.rb', line 208

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

#attributesObject Also known as: to_mongo



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/mongo_mapper/plugins/keys.rb', line 186

def attributes
  attrs = HashWithIndifferentAccess.new

  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

  attrs
end

#attributes=(attrs) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
# File 'lib/mongo_mapper/plugins/keys.rb', line 174

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



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

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

#idObject



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

def id
  _id
end

#id=(value) ⇒ Object



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

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



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/mongo_mapper/plugins/keys.rb', line 158

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



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

def key_names
  keys.keys
end

#keysObject



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

def keys
  self.class.keys
end

#non_embedded_keysObject



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

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

#persisted?Boolean

Returns:



170
171
172
# File 'lib/mongo_mapper/plugins/keys.rb', line 170

def persisted?
  !new? && !destroyed?
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

#update_attributes!(attrs = {}) ⇒ Object



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

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