Class: JsonToToon::DataNode
- Inherits:
-
Object
- Object
- JsonToToon::DataNode
- Defined in:
- lib/json_to_toon/data_node.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#indentation_level ⇒ Object
readonly
Returns the value of attribute indentation_level.
-
#parent ⇒ Object
Returns the value of attribute parent.
Instance Method Summary collapse
- #add_child(child) ⇒ Object
- #indent(spaces = 2) ⇒ Object
-
#initialize(parent = nil) ⇒ DataNode
constructor
A new instance of DataNode.
- #root ⇒ Object
- #to_s ⇒ Object
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_level ⇒ Object (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 |
#parent ⇒ Object
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
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 |
#root ⇒ Object
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_s ⇒ Object
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 |