Class: ActiveRecord::XmlSerializer
- Inherits:
-
Object
- Object
- ActiveRecord::XmlSerializer
show all
- Defined in:
- lib/active_record/xml_serialization.rb
Overview
Defined Under Namespace
Classes: Attribute, MethodAttribute
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(record, options = {}) ⇒ XmlSerializer
Returns a new instance of XmlSerializer.
114
115
116
|
# File 'lib/active_record/xml_serialization.rb', line 114
def initialize(record, options = {})
@record, @options = record, options.dup
end
|
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
112
113
114
|
# File 'lib/active_record/xml_serialization.rb', line 112
def options
@options
end
|
Instance Method Details
#add_attributes ⇒ Object
167
168
169
170
171
|
# File 'lib/active_record/xml_serialization.rb', line 167
def add_attributes
(serializable_attributes + serializable_method_attributes).each do |attribute|
add_tag(attribute)
end
end
|
#add_includes ⇒ Object
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
# File 'lib/active_record/xml_serialization.rb', line 173
def add_includes
if include_associations = options.delete(:include)
root_only_or_except = { :except => options[:except],
:only => options[:only] }
include_has_options = include_associations.is_a?(Hash)
for association in include_has_options ? include_associations.keys : Array(include_associations)
association_options = include_has_options ? include_associations[association] : root_only_or_except
opts = options.merge(association_options)
case @record.class.reflect_on_association(association).macro
when :has_many, :has_and_belongs_to_many
records = @record.send(association).to_a
unless records.empty?
tag = records.first.class.to_s.underscore.pluralize
tag = tag.dasherize if dasherize?
builder.tag!(tag) do
records.each { |r| r.to_xml(opts.merge(:root => association.to_s.singularize)) }
end
end
when :has_one, :belongs_to
if record = @record.send(association)
record.to_xml(opts.merge(:root => association))
end
end
end
options[:include] = include_associations
end
end
|
#add_procs ⇒ Object
207
208
209
210
211
212
213
|
# File 'lib/active_record/xml_serialization.rb', line 207
def add_procs
if procs = options.delete(:procs)
[ *procs ].each do |proc|
proc.call(options)
end
end
end
|
#add_tag(attribute) ⇒ Object
216
217
218
219
220
221
222
|
# File 'lib/active_record/xml_serialization.rb', line 216
def add_tag(attribute)
builder.tag!(
dasherize? ? attribute.name.dasherize : attribute.name,
attribute.value.to_s,
attribute.decorations(!options[:skip_types])
)
end
|
#builder ⇒ Object
118
119
120
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/active_record/xml_serialization.rb', line 118
def builder
@builder ||= begin
options[:indent] ||= 2
builder = options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])
unless options[:skip_instruct]
builder.instruct!
options[:skip_instruct] = true
end
builder
end
end
|
#dasherize? ⇒ Boolean
137
138
139
|
# File 'lib/active_record/xml_serialization.rb', line 137
def dasherize?
!options.has_key?(:dasherize) || options[:dasherize]
end
|
#root ⇒ Object
132
133
134
135
|
# File 'lib/active_record/xml_serialization.rb', line 132
def root
root = (options[:root] || @record.class.to_s.underscore).to_s
dasherize? ? root.dasherize : root
end
|
#serializable_attributes ⇒ Object
To replicate the behavior in ActiveRecord#attributes, :except takes precedence over :only. If :only is not set for a N level model but is set for the N+1 level models, then because :except is set to a default value, the second level model can have both :except and :only set. So if :only is set, always delete :except.
148
149
150
151
152
153
154
155
156
157
158
159
160
|
# File 'lib/active_record/xml_serialization.rb', line 148
def serializable_attributes
attribute_names = @record.attribute_names
if options[:only]
options.delete(:except)
attribute_names = attribute_names & Array(options[:only]).collect { |n| n.to_s }
else
options[:except] = Array(options[:except]) | Array(@record.class.inheritance_column)
attribute_names = attribute_names - options[:except].collect { |n| n.to_s }
end
attribute_names.collect { |name| Attribute.new(name, @record) }
end
|
#serializable_method_attributes ⇒ Object
162
163
164
|
# File 'lib/active_record/xml_serialization.rb', line 162
def serializable_method_attributes
Array(options[:methods]).collect { |name| MethodAttribute.new(name.to_s, @record) }
end
|
#serialize ⇒ Object
Also known as:
to_s
224
225
226
227
228
229
230
231
232
233
234
235
|
# File 'lib/active_record/xml_serialization.rb', line 224
def serialize
args = [root]
if options[:namespace]
args << {:xmlns=>options[:namespace]}
end
builder.tag!(*args) do
add_attributes
add_includes
add_procs
end
end
|