Module Sequel::Plugins::XmlSerializer::InstanceMethods
In: lib/sequel/plugins/xml_serializer.rb

Methods

Public Instance methods

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.

[Source]

     # File lib/sequel/plugins/xml_serializer.rb, line 212
212:         def from_xml(xml, opts=OPTS)
213:           from_xml_node(Nokogiri::XML(xml).children.first, opts)
214:         end

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.

[Source]

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

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.

[Source]

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

[Validate]