Class: Representative::Nokogiri

Inherits:
AbstractXml show all
Defined in:
lib/representative/nokogiri.rb

Overview

Easily generate XML while traversing an object-graph.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractXml

#element, #empty, #list_of

Methods inherited from Base

#current_subject, #representing

Constructor Details

#initialize(subject = nil, options = {}) {|_self| ... } ⇒ Nokogiri

Returns a new instance of Nokogiri.

Yields:

  • (_self)

Yield Parameters:



11
12
13
14
15
16
17
# File 'lib/representative/nokogiri.rb', line 11

def initialize(subject = nil, options = {})
  super(subject, options)
  @doc = ::Nokogiri::XML::Document.new
  @doc.encoding = 'utf-8'
  @current_element = @doc
  yield self if block_given?
end

Instance Attribute Details

#current_elementObject (readonly)

Returns the value of attribute current_element.



19
20
21
# File 'lib/representative/nokogiri.rb', line 19

def current_element
  @current_element
end

#docObject (readonly)

Returns the value of attribute doc.



19
20
21
# File 'lib/representative/nokogiri.rb', line 19

def doc
  @doc
end

Instance Method Details

#attribute(name, value_generator = name) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/representative/nokogiri.rb', line 38

def attribute(name, value_generator = name)
  attribute_name = name.to_s.dasherize
  value = resolve_value(value_generator)
  unless value.nil?
    current_element[attribute_name] = value.to_s
  end
end

#comment(text) ⇒ Object

Generate a comment



33
34
35
36
# File 'lib/representative/nokogiri.rb', line 33

def comment(text)
  comment_node = ::Nokogiri::XML::Comment.new(doc, " #{text} ")
  current_element.add_child(comment_node)
end

#to_sObject



27
28
29
# File 'lib/representative/nokogiri.rb', line 27

def to_s
  to_xml
end

#to_xml(*args) ⇒ Object

Serialize the generated document as XML



23
24
25
# File 'lib/representative/nokogiri.rb', line 23

def to_xml(*args)
  doc.to_xml(*args)
end