Module: MongoMapper::EmbeddedDocument::InstanceMethods

Defined in:
lib/mongomapper/embedded_document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/mongomapper/embedded_document.rb', line 189

def method_missing(method, *args, &block)
  attribute = method.to_s

  if reader?(attribute)
    read_attribute(attribute)
  elsif writer?(attribute)
    write_attribute(attribute.chop, args[0])
  elsif before_typecast_reader?(attribute)
    read_attribute_before_typecast(attribute.gsub(/_before_typecast$/, ''))
  else
    super
  end
end

Instance Attribute Details

#_parentObject (readonly)

The parent document. For root parent is nil



125
126
127
# File 'lib/mongomapper/embedded_document.rb', line 125

def _parent
  @_parent
end

#_parent_keyObject (readonly)

The key name of the embedded document in parent doc



126
127
128
# File 'lib/mongomapper/embedded_document.rb', line 126

def _parent_key
  @_parent_key
end

#_rootObject (readonly)

Metadata attributes for fancy magic e.g. full_key_name e.g. account.login, name.first

user..new? => user.new?
validates_uniqueness_of in embedded document
callbacks in embedded document (TODO)


124
125
126
# File 'lib/mongomapper/embedded_document.rb', line 124

def _root
  @_root
end

Instance Method Details

#==(other) ⇒ Object



203
204
205
# File 'lib/mongomapper/embedded_document.rb', line 203

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

#[](name) ⇒ Object



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

def [](name)
  read_attribute(name)
end

#[]=(name, value) ⇒ Object



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

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

#attributesObject



158
159
160
161
162
163
164
165
# File 'lib/mongomapper/embedded_document.rb', line 158

def attributes
  self.class.keys.inject(HashWithIndifferentAccess.new) do |attributes, key_hash|
    name, key = key_hash
    value = value_for_key(key)
    attributes[name] = value unless value.nil?
    attributes
  end
end

#attributes=(attrs) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
# File 'lib/mongomapper/embedded_document.rb', line 146

def attributes=(attrs)
  return if attrs.blank?
  attrs.each_pair do |key_name, value|
    if writer?(key_name)
      write_attribute(key_name, value)
    else
      writer_method ="#{key_name}="
      self.send(writer_method, value) if respond_to?(writer_method)
    end
  end
end

#before_typecast_reader?(name) ⇒ Boolean

Returns:



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

def before_typecast_reader?(name)
  name.to_s.match(/^(.*)_before_typecast$/) && reader?($1)
end

#full_key_path(name) ⇒ Object



142
143
144
# File 'lib/mongomapper/embedded_document.rb', line 142

def full_key_path(name)
  _parent ? _parent.full_key_path("#{_parent_key}.#{name}") : name
end

#initialize(attrs = {}) ⇒ Object



128
129
130
131
132
133
134
135
136
# File 'lib/mongomapper/embedded_document.rb', line 128

def initialize(attrs={})
  @_root = self.class.include?(MongoMapper::Document) ? self : nil
  @_parent = nil
  @_parent_key = nil
  unless attrs.nil?
    initialize_associations(attrs)
    self.attributes = attrs
  end
end

#inspectObject



207
208
209
210
211
212
# File 'lib/mongomapper/embedded_document.rb', line 207

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

#new?Boolean

Returns:



138
139
140
# File 'lib/mongomapper/embedded_document.rb', line 138

def new?
  _root.try(:new?)
end

#reader?(name) ⇒ Boolean

Returns:



167
168
169
# File 'lib/mongomapper/embedded_document.rb', line 167

def reader?(name)
  defined_key_names.include?(name.to_s)
end

#respond_to?(method, include_private = false) ⇒ Boolean

Returns:



216
217
218
219
# File 'lib/mongomapper/embedded_document.rb', line 216

def respond_to?(method, include_private=false)
  return true if reader?(method) || writer?(method) || before_typecast_reader?(method)
  super
end

#respond_to_without_attributes?Object



214
# File 'lib/mongomapper/embedded_document.rb', line 214

alias :respond_to_without_attributes? :respond_to?

#writer?(name) ⇒ Boolean

Returns:



171
172
173
174
175
# File 'lib/mongomapper/embedded_document.rb', line 171

def writer?(name)
  name = name.to_s
  name = name.chop if name.ends_with?('=')
  reader?(name)
end