Class: JsonToToon::DataNode

Inherits:
Object
  • Object
show all
Defined in:
lib/json_to_toon/data_node.rb

Direct Known Subclasses

ContainerNode, ValueNode

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil) ⇒ DataNode



9
10
11
12
# File 'lib/json_to_toon/data_node.rb', line 9

def initialize(parent = nil)
  @parent = parent
  @indentation_level = parent ? parent.indentation_level + 1 : 0
end

Instance Attribute Details

#indentation_levelObject (readonly)

Returns the value of attribute indentation_level.



7
8
9
# File 'lib/json_to_toon/data_node.rb', line 7

def indentation_level
  @indentation_level
end

#parentObject

Returns the value of attribute parent.



5
6
7
# File 'lib/json_to_toon/data_node.rb', line 5

def parent
  @parent
end

Instance Method Details

#add_child(child) ⇒ Object

Raises:

  • (NotImplementedError)


18
19
20
# File 'lib/json_to_toon/data_node.rb', line 18

def add_child(child)
  raise NotImplementedError, "#{self.class} does not support adding children."
end

#indent(spaces = 2) ⇒ Object



22
23
24
# File 'lib/json_to_toon/data_node.rb', line 22

def indent(spaces = 2)
  ' ' * (@indentation_level * spaces)
end

#rootObject



26
27
28
29
30
# File 'lib/json_to_toon/data_node.rb', line 26

def root
  current = self
  current = current.parent until current.parent.nil?
  current
end

#to_sObject

Raises:

  • (NotImplementedError)


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

def to_s
  raise NotImplementedError, "#{self.class} must implement the 'to_s' method."
end