Class Index [+]

Quicksearch

Sequel::Plugins::XmlSerializer::InstanceMethods

Public Instance Methods

from_xml(xml, opts={}) click to toggle source

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.

     # File lib/sequel/plugins/xml_serializer.rb, line 190
190:         def from_xml(xml, opts={})
191:           from_xml_node(Nokogiri::XML(xml).children.first, opts)
192:         end
from_xml_node(parent, opts={}) click to toggle source

Update the contents of this instance based on the given XML node, which should be a Nokogiri::XML::Node instance.

     # File lib/sequel/plugins/xml_serializer.rb, line 196
196:         def from_xml_node(parent, opts={})
197:           cols = model.columns.map{|x| x.to_s}
198:           assocs = {}
199:           model.associations.map{|x| assocs[x.to_s] = model.association_reflection(x)}
200:           meths = send(:setter_methods, nil, nil)
201:           name_proc = model.xml_deserialize_name_proc(opts)
202:           parent.children.each do |node|
203:             next if node.is_a?(Nokogiri::XML::Text)
204:             k = name_proc[node.name]
205:             if ar = assocs[k]
206:               klass = ar.associated_class
207:               associations[k.to_sym] = if ar.returns_array?
208:                 node.children.reject{|c| c.is_a?(Nokogiri::XML::Text)}.map{|c| klass.from_xml_node(c)}
209:               else
210:                 klass.from_xml_node(node)
211:               end
212:             elsif cols.include?(k)
213:               self[k.to_sym] = node.key?('nil') ? nil : node.children.first.to_s
214:             elsif meths.include?("#{k}=")
215:               send("#{k}=", node.key?('nil') ? nil : node.children.first.to_s)
216:             else
217:               raise Error, "Entry in XML not an association or column and no setter method exists: #{k}"
218:             end
219:           end
220:           self
221:         end
to_xml(opts={}) click to toggle source

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.

     # File lib/sequel/plugins/xml_serializer.rb, line 261
261:         def to_xml(opts={})
262:           vals = values
263:           types = opts[:types]
264:           inc = opts[:include]
265: 
266:           cols = if only = opts[:only]
267:             Array(only)
268:           else
269:             vals.keys - Array(opts[:except])
270:           end
271: 
272:           name_proc = model.xml_serialize_name_proc(opts)
273:           x = model.xml_builder(opts)
274:           x.send(name_proc[opts.fetch(:root_name, model.send(:underscore, model.name).gsub('/', '__')).to_s]) do |x1|
275:             cols.each do |c|
276:               attrs = {}
277:               if types
278:                 attrs[:type] = db_schema.fetch(c, {})[:type]
279:               end
280:               v = vals[c]
281:               if v.nil?
282:                 attrs[:nil] = ''
283:               end
284:               x1.send(name_proc[c.to_s], v, attrs)
285:             end
286:             if inc.is_a?(Hash)
287:               inc.each{|k, v| to_xml_include(x1, k, v)}
288:             else
289:               Array(inc).each{|i| to_xml_include(x1, i)}
290:             end
291:             yield x1 if block_given?
292:           end
293:           x.to_xml
294:         end

Private Instance Methods

to_xml_include(node, i, opts={}) click to toggle source

Handle associated objects and virtual attributes when creating the xml.

     # File lib/sequel/plugins/xml_serializer.rb, line 300
300:         def to_xml_include(node, i, opts={})
301:           name_proc = model.xml_serialize_name_proc(opts)
302:           objs = send(i)
303:           if objs.is_a?(Array) && objs.all?{|x| x.is_a?(Sequel::Model)}
304:             node.send(name_proc[i.to_s]) do |x2|
305:               objs.each{|obj| obj.to_xml(opts.merge(:builder=>x2))}
306:             end
307:           elsif objs.is_a?(Sequel::Model)
308:             objs.to_xml(opts.merge(:builder=>node, :root_name=>i))
309:           else
310:             node.send(name_proc[i.to_s], objs)
311:           end
312:         end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.