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

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

Instance Method Summary collapse

Instance Method Details

#[](name) ⇒ Object



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

def [](name)
  read_key(name)
end

#[]=(name, value) ⇒ Object



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

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

#assign(attrs = {}) ⇒ Object



189
190
191
# File 'lib/mongo_mapper/plugins/keys.rb', line 189

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

#attributesObject Also known as: to_mongo



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

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)
      attrs[association.name] = documents.map { |document| document.to_mongo }
    end
  end

  attrs
end

#attributes=(attrs) ⇒ Object



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

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_keysObject



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

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

#idObject



203
204
205
# File 'lib/mongo_mapper/plugins/keys.rb', line 203

def id
  _id
end

#id=(value) ⇒ Object



207
208
209
210
211
212
213
# File 'lib/mongo_mapper/plugins/keys.rb', line 207

def id=(value)
  if self.class.using_object_id?
    value = MongoMapper.normalize_object_id(value)
  end

  self[:_id] = value
end

#key_namesObject



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

def key_names
  keys.keys
end

#keysObject



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

def keys
  self.class.keys
end

#new?Boolean

Returns:



153
154
155
# File 'lib/mongo_mapper/plugins/keys.rb', line 153

def new?
  @new
end

#non_embedded_keysObject



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

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

#update_attributes(attrs = {}) ⇒ Object



193
194
195
196
# File 'lib/mongo_mapper/plugins/keys.rb', line 193

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

#update_attributes!(attrs = {}) ⇒ Object



198
199
200
201
# File 'lib/mongo_mapper/plugins/keys.rb', line 198

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