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



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

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



7
8
9
10
11
12
# File 'lib/representable/xml/binding.rb', line 7

def Node(document, name, attributes={})
  node = Nokogiri::XML::Node.new(name.to_s, document) # Java::OrgW3cDom::DOMException: NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.

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

Instance Method Details

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



41
42
43
# File 'lib/representable/xml.rb', line 41

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

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



35
36
37
38
39
# File 'lib/representable/xml.rb', line 35

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.



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

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



53
54
55
# File 'lib/representable/xml.rb', line 53

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