Module: MongoMapper::EmbeddedDocument::InstanceMethods

Defined in:
lib/mongomapper/embedded_document.rb

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object



182
183
184
# File 'lib/mongomapper/embedded_document.rb', line 182

def ==(other)
  other.is_a?(self.class) && id == other.id
end

#[](name) ⇒ Object



174
175
176
# File 'lib/mongomapper/embedded_document.rb', line 174

def [](name)
  read_attribute(name)
end

#[]=(name, value) ⇒ Object



178
179
180
# File 'lib/mongomapper/embedded_document.rb', line 178

def []=(name, value)
  write_attribute(name, value)
end

#attributesObject



163
164
165
166
167
168
169
170
171
172
# File 'lib/mongomapper/embedded_document.rb', line 163

def attributes
  returning HashWithIndifferentAccess.new do |attributes|
    self.class.keys.each_pair do |name, key|
      value = value_for_key(key)
      attributes[name] = value unless value.nil?
    end
    
    attributes.merge!(embedded_association_attributes)
  end
end

#attributes=(attrs) ⇒ Object



156
157
158
159
160
161
# File 'lib/mongomapper/embedded_document.rb', line 156

def attributes=(attrs)
  return if attrs.blank?
  attrs.each_pair do |method, value|
    self.send("#{method}=", value)
  end
end

#idObject



186
187
188
# File 'lib/mongomapper/embedded_document.rb', line 186

def id
  self._id.to_s
end

#id=(value) ⇒ Object



190
191
192
# File 'lib/mongomapper/embedded_document.rb', line 190

def id=(value)
  self._id = value
end

#initialize(attrs = {}) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/mongomapper/embedded_document.rb', line 140

def initialize(attrs={})
  unless attrs.nil?
    self.class.associations.each_pair do |name, association|
      if collection = attrs.delete(name)
        send("#{association.name}=", collection)
      end
    end
    
    self.attributes = attrs
  end
  
  if self.class.embeddable? && read_attribute(:_id).blank?
    write_attribute :_id, XGen::Mongo::Driver::ObjectID.new
  end
end

#inspectObject



194
195
196
197
198
199
# File 'lib/mongomapper/embedded_document.rb', line 194

def inspect
  attributes_as_nice_string = self.class.keys.keys.collect do |name|
    "#{name}: #{read_attribute(name)}"
  end.join(", ")
  "#<#{self.class} #{attributes_as_nice_string}>"
end