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 210
210:         def from_xml(xml, opts=OPTS)
211:           from_xml_node(Nokogiri::XML(xml).children.first, opts)
212:         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 224
224:         def from_xml_node(parent, opts=OPTS)
225:           unless parent
226:             raise Error, "Malformed XML used"
227:           end
228:           if !parent.children.empty? && parent.children.all?{|node| node.is_a?(Nokogiri::XML::Text)}
229:             raise Error, "XML consisting of just text nodes used"
230:           end
231: 
232:           if assocs = opts[:associations]
233:             assocs = case assocs
234:             when Symbol
235:               {assocs=>{}}
236:             when Array
237:               assocs_tmp = {}
238:               assocs.each{|v| assocs_tmp[v] = {}}
239:               assocs_tmp
240:             when Hash
241:               assocs
242:             else
243:               raise Error, ":associations should be Symbol, Array, or Hash if present"
244:             end
245: 
246:             assocs_hash = {}
247:             assocs.each{|k,v| assocs_hash[k.to_s] = v}
248:             assocs_present = []
249:           end
250: 
251:           hash = {}
252:           populate_associations = {}
253:           name_proc = model.xml_deserialize_name_proc(opts)
254:           parent.children.each do |node|
255:             next if node.is_a?(Nokogiri::XML::Text)
256:             k = name_proc[node.name]
257:             if assocs_hash && assocs_hash[k]
258:               assocs_present << [k.to_sym, node]
259:             else
260:               hash[k] = node.key?('nil') ? nil : node.children.first.to_s
261:             end
262:           end
263: 
264:           if assocs_present
265:             assocs_present.each do |assoc, node|
266:               assoc_opts = assocs[assoc]
267: 
268:               unless r = model.association_reflection(assoc)
269:                 raise Error, "Association #{assoc} is not defined for #{model}"
270:               end
271: 
272:               populate_associations[assoc] = if r.returns_array?
273:                 node.children.reject{|c| c.is_a?(Nokogiri::XML::Text)}.map{|c| r.associated_class.from_xml_node(c, assoc_opts)}
274:               else
275:                 r.associated_class.from_xml_node(node, assoc_opts)
276:               end
277:             end
278:           end
279: 
280:           if fields = opts[:fields]
281:             set_fields(hash, fields, opts)
282:           else
283:             set(hash)
284:           end
285: 
286:           populate_associations.each do |assoc, values|
287:             associations[assoc] = values
288:           end
289: 
290:           self
291:         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 331
331:         def to_xml(opts=OPTS)
332:           vals = values
333:           types = opts[:types]
334:           inc = opts[:include]
335: 
336:           cols = if only = opts[:only]
337:             Array(only)
338:           else
339:             vals.keys - Array(opts[:except])
340:           end
341: 
342:           name_proc = model.xml_serialize_name_proc(opts)
343:           x = model.xml_builder(opts)
344:           x.send(name_proc[opts.fetch(:root_name, model.send(:underscore, model.name).gsub('/', '__')).to_s]) do |x1|
345:             cols.each do |c|
346:               attrs = {}
347:               if types
348:                 attrs[:type] = db_schema.fetch(c, {})[:type]
349:               end
350:               v = vals[c]
351:               if v.nil?
352:                 attrs[:nil] = ''
353:               end
354:               x1.send(name_proc[c.to_s], v, attrs)
355:             end
356:             if inc.is_a?(Hash)
357:               inc.each{|k, v| to_xml_include(x1, k, v)}
358:             else
359:               Array(inc).each{|i| to_xml_include(x1, i)}
360:             end
361:             yield x1 if block_given?
362:           end
363:           x.to_xml
364:         end

[Validate]