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
149
150
151
152
153
154
155
156
157
158
159
160
161
|
# File 'lib/mongomapper/embedded_document.rb', line 149
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
163
164
165
|
# File 'lib/mongomapper/embedded_document.rb', line 163
def ==(other)
other.is_a?(self.class) && attributes == other.attributes
end
|
#[](name) ⇒ Object
141
142
143
|
# File 'lib/mongomapper/embedded_document.rb', line 141
def [](name)
read_attribute(name)
end
|
#[]=(name, value) ⇒ Object
145
146
147
|
# File 'lib/mongomapper/embedded_document.rb', line 145
def []=(name, value)
write_attribute(name, value)
end
|
#attributes ⇒ Object
118
119
120
121
122
123
124
125
|
# File 'lib/mongomapper/embedded_document.rb', line 118
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
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/mongomapper/embedded_document.rb', line 106
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
137
138
139
|
# File 'lib/mongomapper/embedded_document.rb', line 137
def before_typecast_reader?(name)
name.to_s.match(/^(.*)_before_typecast$/) && reader?($1)
end
|
#initialize(attrs = {}) ⇒ Object
99
100
101
102
103
104
|
# File 'lib/mongomapper/embedded_document.rb', line 99
def initialize(attrs={})
unless attrs.nil?
initialize_associations(attrs)
self.attributes = attrs
end
end
|
#inspect ⇒ Object
167
168
169
170
171
172
|
# File 'lib/mongomapper/embedded_document.rb', line 167
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
127
128
129
|
# File 'lib/mongomapper/embedded_document.rb', line 127
def reader?(name)
defined_key_names.include?(name.to_s)
end
|
#respond_to?(method, include_private = false) ⇒ Boolean
176
177
178
179
|
# File 'lib/mongomapper/embedded_document.rb', line 176
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
174
|
# File 'lib/mongomapper/embedded_document.rb', line 174
alias :respond_to_without_attributes? :respond_to?
|
#writer?(name) ⇒ Boolean
131
132
133
134
135
|
# File 'lib/mongomapper/embedded_document.rb', line 131
def writer?(name)
name = name.to_s
name = name.chop if name.ends_with?('=')
reader?(name)
end
|