Class: Sequel::XmlSerializer
Overview
Defined Under Namespace
Classes: Attribute, MethodAttribute
Constant Summary
collapse
- FOS_XML_OPTIONS =
{:skip_instruct=>true, :dasherize=>false}
Instance Attribute Summary
#options
Instance Method Summary
collapse
#add_includes, #initialize, #serializable_attribute_names, #serializable_method_names, #serializable_names, #serializable_record, #to_s
Instance Method Details
#add_associations(association, records, opts) ⇒ Object
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
# File 'lib/sequel/serializer/xml_serializer.rb', line 239
def add_associations(association, records, opts)
if records.is_a?(Enumerable)
tag = reformat_name(association.to_s)
type = options[:skip_types] ? {} : {:type => "array"}
if records.empty?
builder.tag!(tag, type)
else
builder.tag!(tag, type) do
association_name = association.to_s.singularize
records.each do |record|
if options[:skip_types]
record_type = {}
else
record_class = (record.class.to_s.underscore == association_name) ? nil : record.class.name
record_type = {:type => record_class}
end
record.to_xml opts.merge(:root => association_name).merge(record_type)
end
end
end
else
if record = @record.send(association)
record.to_xml(opts.merge(:root => association))
end
end
end
|
#add_attributes ⇒ Object
217
218
219
220
221
|
# File 'lib/sequel/serializer/xml_serializer.rb', line 217
def add_attributes
(serializable_attributes + serializable_method_attributes).each do |attribute|
add_tag(attribute)
end
end
|
#add_procs ⇒ Object
223
224
225
226
227
228
229
|
# File 'lib/sequel/serializer/xml_serializer.rb', line 223
def add_procs
if procs = options.delete(:procs)
[ *procs ].each do |proc|
proc.call(options)
end
end
end
|
#add_tag(attribute) ⇒ Object
231
232
233
234
235
236
237
|
# File 'lib/sequel/serializer/xml_serializer.rb', line 231
def add_tag(attribute)
builder.tag!(
reformat_name(attribute.name),
attribute.value.to_s,
attribute.decorations(!options[:skip_types], !options[:skip_nils])
)
end
|
174
175
176
177
178
179
180
181
182
183
184
185
186
|
# File 'lib/sequel/serializer/xml_serializer.rb', line 174
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
|
#camelize? ⇒ Boolean
197
198
199
|
# File 'lib/sequel/serializer/xml_serializer.rb', line 197
def camelize?
options.has_key?(:camelize) && options[:camelize]
end
|
#dasherize? ⇒ Boolean
193
194
195
|
# File 'lib/sequel/serializer/xml_serializer.rb', line 193
def dasherize?
!options.has_key?(:dasherize) || options[:dasherize]
end
|
201
202
203
204
|
# File 'lib/sequel/serializer/xml_serializer.rb', line 201
def reformat_name(name)
name = name.camelize if camelize?
name = dasherize? ? name.to_s.dasherize : name.to_s
end
|
188
189
190
191
|
# File 'lib/sequel/serializer/xml_serializer.rb', line 188
def root
root = (options[:root] || @record.class.to_s.underscore).to_s
reformat_name(root)
end
|
#serializable_attributes ⇒ Object
206
207
208
|
# File 'lib/sequel/serializer/xml_serializer.rb', line 206
def serializable_attributes
serializable_attribute_names.collect { |name| Attribute.new(name, @record) }
end
|
#serializable_method_attributes ⇒ Object
210
211
212
213
214
215
|
# File 'lib/sequel/serializer/xml_serializer.rb', line 210
def serializable_method_attributes
Array(options[:methods]).inject([]) do |method_attributes, name|
method_attributes << MethodAttribute.new(name.to_s, @record) method_attributes
end
end
|
#serialize ⇒ Object
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
# File 'lib/sequel/serializer/xml_serializer.rb', line 268
def serialize
args = [root]
if options[:namespace]
args << {:xmlns=>options[:namespace]}
end
if options[:type]
args << {:type=>options[:type]}
end
@tag_names = []
builder.tag!(*args) do
add_attributes
procs = options.delete(:procs)
add_includes { |association, records, opts| @tag_names << association; add_associations(association, records, opts) }
options[:procs] = procs
add_procs
yield builder if block_given?
end
end
|