Module: MongoMapper::EmbeddedDocument::InstanceMethods
- Defined in:
- lib/mongomapper/embedded_document.rb
Instance Method Summary collapse
- #==(other) ⇒ Object
- #[](name) ⇒ Object
- #[]=(name, value) ⇒ Object
- #attributes ⇒ Object
- #attributes=(attrs) ⇒ Object
- #id ⇒ Object
- #id=(value) ⇒ Object
- #initialize(attrs = {}) ⇒ Object
- #inspect ⇒ Object
- #new_record? ⇒ Boolean
- #using_custom_id? ⇒ Boolean
Instance Method Details
#==(other) ⇒ Object
225 226 227 |
# File 'lib/mongomapper/embedded_document.rb', line 225 def ==(other) other.is_a?(self.class) && id == other.id end |
#[](name) ⇒ Object
216 217 218 |
# File 'lib/mongomapper/embedded_document.rb', line 216 def [](name) read_attribute(name) end |
#[]=(name, value) ⇒ Object
220 221 222 223 |
# File 'lib/mongomapper/embedded_document.rb', line 220 def []=(name, value) ensure_key_exists(name) write_attribute(name, value) end |
#attributes ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/mongomapper/embedded_document.rb', line 199 def attributes attrs = HashWithIndifferentAccess.new self.class.keys.each_pair do |name, key| value = if key.native? read_attribute(key.name) else if = read_attribute(key.name) .attributes end end attrs[name] = value unless value.nil? end attrs.merge!() end |
#attributes=(attrs) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/mongomapper/embedded_document.rb', line 186 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 |
#id ⇒ Object
229 230 231 |
# File 'lib/mongomapper/embedded_document.rb', line 229 def id read_attribute(:_id) end |
#id=(value) ⇒ Object
233 234 235 236 |
# File 'lib/mongomapper/embedded_document.rb', line 233 def id=(value) @using_custom_id = true write_attribute :_id, value end |
#initialize(attrs = {}) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/mongomapper/embedded_document.rb', line 157 def initialize(attrs={}) unless attrs.nil? self.class.associations.each_pair do |name, association| if collection = attrs.delete(name) if association.many? && association.klass. parent_document = attrs[:_parent_document] || self collection.each do |doc| doc[:_parent_document] = parent_document end end send("#{association.name}=", collection) end end self.attributes = attrs end if self.class. if read_attribute(:_id).blank? write_attribute :_id, XGen::Mongo::Driver::ObjectID.new.to_s @new_record = true end end end |
#inspect ⇒ Object
242 243 244 245 246 247 |
# File 'lib/mongomapper/embedded_document.rb', line 242 def inspect attributes_as_nice_string = self.class.keys.keys.collect do |name| "#{name}: #{read_attribute(name).inspect}" end.join(", ") "#<#{self.class} #{attributes_as_nice_string}>" end |
#new_record? ⇒ Boolean
182 183 184 |
# File 'lib/mongomapper/embedded_document.rb', line 182 def new_record? !!@new_record end |
#using_custom_id? ⇒ Boolean
238 239 240 |
# File 'lib/mongomapper/embedded_document.rb', line 238 def using_custom_id? !!@using_custom_id end |