Module: MongoMapper::EmbeddedDocument::InstanceMethods

Defined in:
lib/mongomapper/embedded_document.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/mongomapper/embedded_document.rb', line 148

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 Method Details

#==(other) ⇒ Object



162
163
164
# File 'lib/mongomapper/embedded_document.rb', line 162

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

#[](name) ⇒ Object



140
141
142
# File 'lib/mongomapper/embedded_document.rb', line 140

def [](name)
  read_attribute(name)
end

#[]=(name, value) ⇒ Object



144
145
146
# File 'lib/mongomapper/embedded_document.rb', line 144

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

#attributesObject



117
118
119
120
121
122
123
124
# File 'lib/mongomapper/embedded_document.rb', line 117

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



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/mongomapper/embedded_document.rb', line 105

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:



136
137
138
# File 'lib/mongomapper/embedded_document.rb', line 136

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

#initialize(attrs = {}) ⇒ Object



98
99
100
101
102
103
# File 'lib/mongomapper/embedded_document.rb', line 98

def initialize(attrs={})
  unless attrs.nil?
    initialize_associations(attrs)
    self.attributes = attrs
  end
end

#inspectObject



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

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

#reader?(name) ⇒ Boolean

Returns:



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

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

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

Returns:



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

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



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

alias :respond_to_without_attributes? :respond_to?

#writer?(name) ⇒ Boolean

Returns:



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

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