Class: JSONP3::Path::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/json_p3/path/node.rb

Overview

A JSON-like value and its location.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, location, root) ⇒ Node

Returns a new instance of Node.

Parameters:

  • value (JSON-like)

    the value at this node.

  • location (Array<String | Integer | Array<String | Integer>>)

    the sequence of names and/or indices leading to value in root.

  • root (JSON-like)

    the root value containing value at location.



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

def initialize(value, location, root)
  @value = value
  @location = location
  @root = root
end

Instance Attribute Details

#locationObject (readonly)

Returns the value of attribute location.



10
11
12
# File 'lib/json_p3/path/node.rb', line 10

def location
  @location
end

#rootObject (readonly)

Returns the value of attribute root.



10
11
12
# File 'lib/json_p3/path/node.rb', line 10

def root
  @root
end

#valueObject (readonly)

Returns the value of attribute value.



10
11
12
# File 'lib/json_p3/path/node.rb', line 10

def value
  @value
end

Instance Method Details

#new_child(value, key) ⇒ Object

Return a new node that is a child of this node.

Parameters:

  • value

    the JSON-like value at the new node.

  • key (Integer, String)

    the array index or hash key associated with value.



34
35
36
# File 'lib/json_p3/path/node.rb', line 34

def new_child(value, key)
  Node.new(value, [@location, key], @root)
end

#pathString

Return the normalized path to this node.

Returns:

  • (String)

    the normalized path.



24
25
26
27
28
29
# File 'lib/json_p3/path/node.rb', line 24

def path
  segments = @location.flatten.map do |i|
    i.is_a?(String) ? "[#{JSONP3::Path.canonical_string(i)}]" : "[#{i}]"
  end
  "$#{segments.join}"
end

#to_sObject



38
39
40
# File 'lib/json_p3/path/node.rb', line 38

def to_s
  "Node(#{value} at #{path})"
end