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 219
219:         def from_xml(xml, opts={})
220:           if opts[:all_associations] || opts[:all_columns]
221:             Sequel::Deprecation.deprecate("The from_xml :all_associations and :all_columns", 'You need to explicitly specify the associations and columns via the :associations and :fields options')
222:           end
223:           from_xml_node(Nokogiri::XML(xml).children.first, opts)
224:         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:

:all_associations :Indicates that all associations supported by the model should be tried. This option also cascades to associations if used. It is better to use the :associations option instead of this option. This option only exists for backwards compatibility.
:all_columns :Overrides the setting logic allowing all setter methods be used, even if access to the setter method is restricted. This option cascades to associations if used, and can be reset in those associations using the :all_columns=>false or :fields options. This option is considered a security risk, and only exists for backwards compatibility. It is better to use the :fields option appropriately instead of this option, or no option at all.
: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 246
246:         def from_xml_node(parent, opts={})
247:           unless parent
248:             raise Error, "Malformed XML used"
249:           end
250:           if !parent.children.empty? && parent.children.all?{|node| node.is_a?(Nokogiri::XML::Text)}
251:             raise Error, "XML consisting of just text nodes used"
252:           end
253: 
254:           unless assocs = opts[:associations]
255:             if opts[:all_associations]
256:               assocs = {}
257:               model.associations.each{|v| assocs[v] = {:all_associations=>true}}
258:             end
259:           end
260: 
261:           if assocs
262:             assocs = case assocs
263:             when Symbol
264:               {assocs=>{}}
265:             when Array
266:               assocs_tmp = {}
267:               assocs.each{|v| assocs_tmp[v] = {}}
268:               assocs_tmp
269:             when Hash
270:               assocs
271:             else
272:               raise Error, ":associations should be Symbol, Array, or Hash if present"
273:             end
274: 
275:             if opts[:all_columns]
276:               assocs.each_value do |assoc_opts|
277:                 assoc_opts[:all_columns] = true unless assoc_opts.has_key?(:fields) || assoc_opts.has_key?(:all_columns)
278:               end
279:             end
280: 
281:             assocs_hash = {}
282:             assocs.each{|k,v| assocs_hash[k.to_s] = v}
283:             assocs_present = []
284:           end
285: 
286:           hash = {}
287:           name_proc = model.xml_deserialize_name_proc(opts)
288:           parent.children.each do |node|
289:             next if node.is_a?(Nokogiri::XML::Text)
290:             k = name_proc[node.name]
291:             if assocs_hash && assocs_hash[k]
292:               assocs_present << [k.to_sym, node]
293:             else
294:               hash[k] = node.key?('nil') ? nil : node.children.first.to_s
295:             end
296:           end
297: 
298:           if assocs_present
299:             assocs_present.each do |assoc, node|
300:               assoc_opts = assocs[assoc]
301: 
302:               unless r = model.association_reflection(assoc)
303:                 raise Error, "Association #{assoc} is not defined for #{model}"
304:               end
305: 
306:               associations[assoc] = if r.returns_array?
307:                 node.children.reject{|c| c.is_a?(Nokogiri::XML::Text)}.map{|c| r.associated_class.from_xml_node(c, assoc_opts)}
308:               else
309:                 r.associated_class.from_xml_node(node, assoc_opts)
310:               end
311:             end
312:           end
313: 
314:           if fields = opts[:fields]
315:             set_fields(hash, fields, opts)
316:           elsif opts[:all_columns]
317:             meths = methods.collect{|x| x.to_s}.grep(Model::SETTER_METHOD_REGEXP) - Model::RESTRICTED_SETTER_METHODS
318:             hash.each do |k, v|
319:               if meths.include?(setter_meth = "#{k}=")
320:                 send(setter_meth, v)
321:               else
322:                 raise Error, "Entry in XML does not have a matching setter method: #{k}"
323:               end
324:             end
325:           else
326:             set(hash)
327:           end
328: 
329:           self
330:         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 370
370:         def to_xml(opts={})
371:           vals = values
372:           types = opts[:types]
373:           inc = opts[:include]
374: 
375:           cols = if only = opts[:only]
376:             Array(only)
377:           else
378:             vals.keys - Array(opts[:except])
379:           end
380: 
381:           name_proc = model.xml_serialize_name_proc(opts)
382:           x = model.xml_builder(opts)
383:           x.send(name_proc[opts.fetch(:root_name, model.send(:underscore, model.name).gsub('/', '__')).to_s]) do |x1|
384:             cols.each do |c|
385:               attrs = {}
386:               if types
387:                 attrs[:type] = db_schema.fetch(c, {})[:type]
388:               end
389:               v = vals[c]
390:               if v.nil?
391:                 attrs[:nil] = ''
392:               end
393:               x1.send(name_proc[c.to_s], v, attrs)
394:             end
395:             if inc.is_a?(Hash)
396:               inc.each{|k, v| to_xml_include(x1, k, v)}
397:             else
398:               Array(inc).each{|i| to_xml_include(x1, i)}
399:             end
400:             yield x1 if block_given?
401:           end
402:           x.to_xml
403:         end

[Validate]