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
168
169
170
171
172
173
174
175
176
177
178
179
180
|
# File 'lib/mongomapper/embedded_document.rb', line 168
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
182
183
184
|
# File 'lib/mongomapper/embedded_document.rb', line 182
def ==(other)
other.is_a?(self.class) && attributes == other.attributes
end
|
#[](name) ⇒ Object
160
161
162
|
# File 'lib/mongomapper/embedded_document.rb', line 160
def [](name)
read_attribute(name)
end
|
#[]=(name, value) ⇒ Object
164
165
166
|
# File 'lib/mongomapper/embedded_document.rb', line 164
def []=(name, value)
write_attribute(name, value)
end
|
#attributes ⇒ Object
137
138
139
140
141
142
143
144
|
# File 'lib/mongomapper/embedded_document.rb', line 137
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
125
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/mongomapper/embedded_document.rb', line 125
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
156
157
158
|
# File 'lib/mongomapper/embedded_document.rb', line 156
def before_typecast_reader?(name)
name.to_s.match(/^(.*)_before_typecast$/) && reader?($1)
end
|
#initialize(attrs = {}) ⇒ Object
118
119
120
121
122
123
|
# File 'lib/mongomapper/embedded_document.rb', line 118
def initialize(attrs={})
unless attrs.nil?
initialize_associations(attrs)
self.attributes = attrs
end
end
|
#inspect ⇒ Object
186
187
188
189
190
191
|
# File 'lib/mongomapper/embedded_document.rb', line 186
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
146
147
148
|
# File 'lib/mongomapper/embedded_document.rb', line 146
def reader?(name)
defined_key_names.include?(name.to_s)
end
|
#respond_to?(method, include_private = false) ⇒ Boolean
195
196
197
198
|
# File 'lib/mongomapper/embedded_document.rb', line 195
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
193
|
# File 'lib/mongomapper/embedded_document.rb', line 193
alias :respond_to_without_attributes? :respond_to?
|
#writer?(name) ⇒ Boolean
150
151
152
153
154
|
# File 'lib/mongomapper/embedded_document.rb', line 150
def writer?(name)
name = name.to_s
name = name.chop if name.ends_with?('=')
reader?(name)
end
|