Class: Node

Inherits:
Object
  • Object
show all
Includes:
Indentation
Defined in:
lib/node.rb

Constant Summary collapse

@@json_formatter =
JsonNodeFormatter.new
@@xml_formatter =
XmlNodeFormatter.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Indentation

#indent, #unindent

Constructor Details

#initialize(label) ⇒ Node

Returns a new instance of Node.



16
17
18
19
# File 'lib/node.rb', line 16

def initialize(label)
  @label = label
  @children = Nodes.new
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



14
15
16
# File 'lib/node.rb', line 14

def children
  @children
end

#labelObject (readonly)

Returns the value of attribute label.



13
14
15
# File 'lib/node.rb', line 13

def label
  @label
end

Instance Method Details

#to_jsonObject



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_xmlObject



46
47
48
# File 'lib/node.rb', line 46

def to_xml
  @@xml_formatter.format self
end