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

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

Instance Method Summary collapse

Instance Method Details

#[](name) ⇒ Object



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

def [](name)
  read_key(name)
end

#[]=(name, value) ⇒ Object



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

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

#assign(attrs = {}) ⇒ Object



206
207
208
# File 'lib/mongo_mapper/plugins/keys.rb', line 206

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

#attributesObject Also known as: to_mongo



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

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



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)
  return if attrs.blank?

  attrs.each_pair do |name, value|
    writer_method = "#{name}="

    if respond_to?(writer_method)
      if writer_method == '_root_document='
        puts "_root_document= #{value.inspect}"
      end
      self.send(writer_method, value)
    else
      self[name.to_s] = 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



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

def id
  _id
end

#id=(value) ⇒ Object



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

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

  self[:_id] = value
end

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



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/mongo_mapper/plugins/keys.rb', line 148

def initialize(attrs={}, from_database=false)
  unless attrs.nil?
    provided_keys = attrs.keys.map { |k| k.to_s }
    unless provided_keys.include?('_id') || provided_keys.include?('id')
      write_key :_id, Mongo::ObjectID.new
    end
  end

  assign_type_if_present

  if from_database
    @new = false
    self.attributes = 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



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

def keys
  self.class.keys
end

#new?Boolean

Returns:



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

def new?
  @new
end

#non_embedded_keysObject



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

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

#update_attributes(attrs = {}) ⇒ Object



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

def update_attributes(attrs={})
  assign(attrs)
  save
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