Class: Representative::Nokogiri
- Inherits:
-
AbstractXml
- Object
- Base
- AbstractXml
- Representative::Nokogiri
- Defined in:
- lib/representative/nokogiri.rb
Overview
Easily generate XML while traversing an object-graph.
Instance Attribute Summary collapse
-
#current_element ⇒ Object
readonly
Returns the value of attribute current_element.
-
#doc ⇒ Object
readonly
Returns the value of attribute doc.
Instance Method Summary collapse
- #attribute(name, value_generator = name) ⇒ Object
-
#comment(text) ⇒ Object
Generate a comment.
-
#initialize(subject = nil, options = {}) {|_self| ... } ⇒ Nokogiri
constructor
A new instance of Nokogiri.
- #to_s ⇒ Object
-
#to_xml(*args) ⇒ Object
Serialize the generated document as XML.
Methods inherited from AbstractXml
Methods inherited from Base
#current_subject, #representing
Constructor Details
#initialize(subject = nil, options = {}) {|_self| ... } ⇒ Nokogiri
Returns a new instance of Nokogiri.
11 12 13 14 15 16 17 |
# File 'lib/representative/nokogiri.rb', line 11 def initialize(subject = nil, = {}) super(subject, ) @doc = ::Nokogiri::XML::Document.new @doc.encoding = 'utf-8' @current_element = @doc yield self if block_given? end |
Instance Attribute Details
#current_element ⇒ Object (readonly)
Returns the value of attribute current_element.
19 20 21 |
# File 'lib/representative/nokogiri.rb', line 19 def current_element @current_element end |
#doc ⇒ Object (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_s ⇒ Object
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 |