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

[Validate]