Class: PerfectTOML::Parser::Node

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

Overview

object builder

Constant Summary collapse

Terminal =
Node.new(:defined_as_value, nil)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, parent) ⇒ Node

This is an internal data structure to create a Ruby object from TOML. A node corresponds to a table in TOML.

There are five node types:

  1. declared

Declared as a table by a dotted-key header, but not defined yet.
This type may be changed to "1. defined_by_header".

Example 1: "a" and "a.b" of "[a.b.c]"
Example 2: "a" and "a.b" of "[[a.b.c]]".
  1. defined_by_header

Defined as a table by a header. This type is final.

Example: "a.b.c" of "[a.b.c]"
  1. defined_by_dot

Defined as a table by a dotted-key value definition.
This type is final.

Example: "a" and "a.b" of "a.b.c = val"

Note: we need to distinguish between defined_by_header and
defined_by_dot because defined_by_dot can modify a table of
defined_by_dot:

    a.b.c=1  # define "a.b" as defined_by_dot
    a.b.d=2  # able to modify "a.b" (add "d" to "a.b")

but cannot modify a table of defined_by_header:

    [a.b]    # define "a.b" as defined_by_header
    c=1
    [a]
    b.d=2    # unable to modify "a.b"
  1. defined_as_array

Defined as an array of tables. This type is final, but this node
may be replaced with a new element of the array. A node has a
reference to the last table in the array.

Example: "a.b.c" of "[[a.b.c]]"
  1. defined_as_value

Defined as a value. This type is final.

Example: "a.b.c" of "a.b.c = val"


635
636
637
638
639
640
# File 'lib/perfect_toml.rb', line 635

def initialize(type, parent)
  @type = type
  @children = {}
  @table = {}
  @parent = parent
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



643
644
645
# File 'lib/perfect_toml.rb', line 643

def children
  @children
end

#tableObject (readonly)

Returns the value of attribute table.



643
644
645
# File 'lib/perfect_toml.rb', line 643

def table
  @table
end

#typeObject

Returns the value of attribute type.



642
643
644
# File 'lib/perfect_toml.rb', line 642

def type
  @type
end

Instance Method Details

#pathObject



647
648
649
650
651
# File 'lib/perfect_toml.rb', line 647

def path
  return [] unless @parent
  key, = @parent.children.find {|key, child| child == self }
  @parent.path + [key]
end