module Sequel::Plugins::XmlSerializer::InstanceMethods

  1. lib/sequel/plugins/xml_serializer.rb

Methods

Public Instance

  1. from_xml
  2. from_xml_node
  3. to_xml

Public Instance methods

from_xml(xml, opts=OPTS)

Update the contents of this instance based on the given XML. Accepts the following options:

:name_proc

Proc or Hash that accepts a string and returns a string, used to convert tag names to column or association names.

:underscore

Sets the :name_proc option to one that calls underscore on the input string. Requires that you load the inflector extension or another library that adds String#underscore.

[show source]
    # File lib/sequel/plugins/xml_serializer.rb
213 def from_xml(xml, opts=OPTS)
214   from_xml_node(Nokogiri::XML(xml).children.first, opts)
215 end
from_xml_node(parent, opts=OPTS)

Update the contents of this instance based on the given XML node, which should be a Nokogiri::XML::Node instance. By default, just calls set with a hash created from the content of the node.

Options:

:associations

Indicates that the associations cache should be updated by creating a new associated object using data from the hash. Should be a Symbol for a single association, an array of symbols for multiple associations, or a hash with symbol keys and dependent association option hash values.

:fields

Changes the behavior to call set_fields using the provided fields, instead of calling set.

[show source]
    # File lib/sequel/plugins/xml_serializer.rb
227 def from_xml_node(parent, opts=OPTS)
228   unless parent
229     raise Error, "Malformed XML used"
230   end
231   if !parent.children.empty? && parent.children.all?{|node| node.is_a?(Nokogiri::XML::Text)}
232     raise Error, "XML consisting of just text nodes used"
233   end
234 
235   if assocs = opts[:associations]
236     assocs = case assocs
237     when Symbol
238       {assocs=>OPTS}
239     when Array
240       assocs_tmp = {}
241       assocs.each{|v| assocs_tmp[v] = OPTS}
242       assocs_tmp
243     when Hash
244       assocs
245     else
246       raise Error, ":associations should be Symbol, Array, or Hash if present"
247     end
248 
249     assocs_hash = {}
250     assocs.each{|k,v| assocs_hash[k.to_s] = v}
251     assocs_present = []
252   end
253 
254   hash = {}
255   populate_associations = {}
256   name_proc = model.xml_deserialize_name_proc(opts)
257   parent.children.each do |node|
258     next if node.is_a?(Nokogiri::XML::Text)
259     k = name_proc[node.name]
260     if assocs_hash && assocs_hash[k]
261       assocs_present << [k.to_sym, node]
262     else
263       hash[k] = node.key?('nil') ? nil : node.children.first.to_s
264     end
265   end
266 
267   if assocs_present
268     assocs_present.each do |assoc, node|
269       assoc_opts = assocs[assoc]
270 
271       unless r = model.association_reflection(assoc)
272         raise Error, "Association #{assoc} is not defined for #{model}"
273       end
274 
275       populate_associations[assoc] = if r.returns_array?
276         node.children.reject{|c| c.is_a?(Nokogiri::XML::Text)}.map{|c| r.associated_class.from_xml_node(c, assoc_opts)}
277       else
278         r.associated_class.from_xml_node(node, assoc_opts)
279       end
280     end
281   end
282 
283   if fields = opts[:fields]
284     set_fields(hash, fields, opts)
285   else
286     set(hash)
287   end
288 
289   populate_associations.each do |assoc, values|
290     associations[assoc] = values
291   end
292 
293   self
294 end
to_xml(opts=OPTS)

Return a string in XML format. If a block is given, yields the XML builder object so you can add additional XML tags. Accepts the following options:

:builder

The builder instance used to build the XML, which should be an instance of Nokogiri::XML::Node. This is necessary if you are serializing entire object graphs, like associated objects.

:builder_opts

Options to pass to the Nokogiri::XML::Builder initializer, if the :builder option is not provided.

:camelize

Sets the :name_proc option to one that calls camelize on the input string. Requires that you load the inflector extension or another library that adds String#camelize.

:dasherize

Sets the :name_proc option to one that calls dasherize on the input string. Requires that you load the inflector extension or another library that adds String#dasherize.

:encoding

The encoding to use for the XML output, passed to the Nokogiri::XML::Builder initializer.

:except

Symbol or Array of Symbols of columns not to include in the XML output.

:include

Symbol, Array of Symbols, or a Hash with Symbol keys and Hash values specifying associations or other non-column attributes to include in the XML output. Using a nested hash, you can pass options to associations to affect the XML used for associated objects.

:name_proc

Proc or Hash that accepts a string and returns a string, used to format tag names.

:only

Symbol or Array of Symbols of columns to only include in the JSON output, ignoring all other columns.

:root_name

The base name to use for the XML tag that contains the data for this instance. This will be the name of the root node if you are only serializing a single object, but not if you are serializing an array of objects using Model.to_xml or Dataset#to_xml.

:types

Set to true to include type information for all of the columns, pulled from the db_schema.

[show source]
    # File lib/sequel/plugins/xml_serializer.rb
334 def to_xml(opts=OPTS)
335   vals = values
336   types = opts[:types]
337   inc = opts[:include]
338 
339   cols = if only = opts[:only]
340     Array(only)
341   else
342     vals.keys - Array(opts[:except])
343   end
344 
345   name_proc = model.xml_serialize_name_proc(opts)
346   x = model.xml_builder(opts)
347   x.public_send(name_proc[opts.fetch(:root_name, model.send(:underscore, model.name).gsub('/', '__')).to_s]) do |x1|
348     cols.each do |c|
349       attrs = {}
350       if types
351         attrs[:type] = db_schema.fetch(c, OPTS)[:type]
352       end
353       v = vals[c]
354       if v.nil?
355         attrs[:nil] = ''
356       end
357       x1.public_send(name_proc[c.to_s], v, attrs)
358     end
359     if inc.is_a?(Hash)
360       inc.each{|k, v| to_xml_include(x1, k, v)}
361     else
362       Array(inc).each{|i| to_xml_include(x1, i)}
363     end
364     yield x1 if defined?(yield)
365   end
366   x.to_xml
367 end