Class: Node
Constant Summary collapse
- @@json_formatter =
JsonNodeFormatter.new
- @@xml_formatter =
XmlNodeFormatter.new
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#label ⇒ Object
readonly
Returns the value of attribute label.
Instance Method Summary collapse
-
#initialize(label) ⇒ Node
constructor
A new instance of Node.
- #to_json ⇒ Object
- #to_s(indent_level = 0, &formatter_block) ⇒ Object
- #to_xml ⇒ Object
Methods included from Indentation
Constructor Details
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
14 15 16 |
# File 'lib/node.rb', line 14 def children @children end |
#label ⇒ Object (readonly)
Returns the value of attribute label.
13 14 15 |
# File 'lib/node.rb', line 13 def label @label end |
Instance Method Details
#to_json ⇒ Object
42 43 44 |
# File 'lib/node.rb', line 42 def to_json @@json_formatter.format self end |
#to_s(indent_level = 0, &formatter_block) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/node.rb', line 21 def to_s(indent_level = 0, &formatter_block) if not block_given? return to_s(indent_level) { |node| node.label } end state = FormatterState.new case formatter_block.arity when 1 s = indent(formatter_block.call(self), indent_level) when 2 s = indent(formatter_block.call([self, state]), indent_level) end unless state.formatted_children? s << "\n" unless @children.empty? s << @children.to_s(indent_level + 1, &formatter_block) end s end |
#to_xml ⇒ Object
46 47 48 |
# File 'lib/node.rb', line 46 def to_xml @@xml_formatter.format self end |