Module: Sequel::Plugins::XmlSerializer::InstanceMethods

Defined in:
lib/sequel/plugins/xml_serializer.rb

Instance Method Summary collapse

Instance Method Details

#from_xml(xml, opts = {}) ⇒ Object

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.



185
186
187
# File 'lib/sequel/plugins/xml_serializer.rb', line 185

def from_xml(xml, opts={})
  from_xml_node(Nokogiri::XML(xml).children.first, opts)
end

#from_xml_node(parent, opts = {}) ⇒ Object

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



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/sequel/plugins/xml_serializer.rb', line 191

def from_xml_node(parent, opts={})
  cols = model.columns.map{|x| x.to_s}
  assocs = {}
  model.associations.map{|x| assocs[x.to_s] = model.association_reflection(x)}
  meths = send(:setter_methods, nil, nil)
  name_proc = model.xml_deserialize_name_proc(opts)
  parent.children.each do |node|
    next if node.is_a?(Nokogiri::XML::Text)
    k = name_proc[node.name]
    if ar = assocs[k]
      klass = ar.associated_class
      associations[k.to_sym] = if ar.returns_array?
        node.children.reject{|c| c.is_a?(Nokogiri::XML::Text)}.map{|c| klass.from_xml_node(c)}
      else
        klass.from_xml_node(node)
      end
    elsif cols.include?(k)
      self[k.to_sym] = node.children.first.to_s
    elsif meths.include?("#{k}=")
      send("#{k}=", node.children.first.to_s)
    else
      raise Error, "Entry in XML not an association or column and no setter method exists: #{k}"
    end
  end
  self
end

#to_xml(opts = {}) ⇒ Object

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.



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/sequel/plugins/xml_serializer.rb', line 256

def to_xml(opts={})
  vals = values
  types = opts[:types]
  inc = opts[:include]

  cols = if only = opts[:only]
    Array(only)
  else
    vals.keys - Array(opts[:except])
  end

  name_proc = model.xml_serialize_name_proc(opts)
  x = model.xml_builder(opts)
  x.send(name_proc[opts.fetch(:root_name, model.send(:underscore, model.name)).to_s]) do |x1|
    cols.each do |c|
      x1.send(name_proc[c.to_s], vals[c], types ? {:type=>db_schema.fetch(c, {})[:type]} : {})
    end
    if inc.is_a?(Hash)
      inc.each{|k, v| to_xml_include(x1, k, v)}
    else
      Array(inc).each{|i| to_xml_include(x1, i)}
    end
    yield x1 if block_given?
  end
  x.to_xml
end