Module: Representable::XML

Included in:
AttributeHash, Hash
Defined in:
lib/representable/xml.rb,
lib/representable/xml/hash.rb,
lib/representable/xml/binding.rb,
lib/representable/xml/namespace.rb,
lib/representable/xml/collection.rb

Defined Under Namespace

Modules: AttributeHash, ClassMethods, Collection, Hash, Namespace Classes: Binding

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/representable/xml.rb', line 19

def self.included(base)
  base.class_eval do
    include Representable
    extend ClassMethods
    self.representation_wrap = true # let representable compute it.
    register_feature Representable::XML
  end
end

.Node(document, name, attributes = {}) ⇒ Object



12
13
14
15
16
17
# File 'lib/representable/xml.rb', line 12

module_function def Node(document, name, attributes={})
  node = document.create_element(name.to_s)

  attributes.each { |k, v| node[k] = v } # TODO: benchmark.
  node
end

Instance Method Details

#from_node(node, options = {}) ⇒ Object



49
50
51
# File 'lib/representable/xml.rb', line 49

def from_node(node, options={})
  update_properties_from(node, options, Binding)
end

#from_xml(doc, *args) ⇒ Object Also known as: parse



43
44
45
46
47
# File 'lib/representable/xml.rb', line 43

def from_xml(doc, *args)
  node = parse_xml(doc, *args)

  from_node(node, *args)
end

#to_node(options = {}) ⇒ Object

Returns a Nokogiri::XML object representing this object.



54
55
56
57
58
59
# File 'lib/representable/xml.rb', line 54

def to_node(options={})
  options[:doc] = Nokogiri::XML::Document.new # DISCUSS: why do we need a fresh Document here?
  root_tag = options[:wrap] || representation_wrap(options)

  create_representation_with(Node(options[:doc], root_tag.to_s), options, Binding)
end

#to_xml(*args) ⇒ Object Also known as: render



61
62
63
# File 'lib/representable/xml.rb', line 61

def to_xml(*args)
  to_node(*args).to_s
end