Class: JSONP3::JSONPathNode

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

Overview

A JSON-like value and its location.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, location, root) ⇒ JSONPathNode

Returns a new instance of JSONPathNode.

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.



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

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

Instance Attribute Details

#locationObject (readonly)

Returns the value of attribute location.



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

def location
  @location
end

#rootObject (readonly)

Returns the value of attribute root.



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

def root
  @root
end

#valueObject (readonly)

Returns the value of attribute value.



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

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.



31
32
33
# File 'lib/json_p3/node.rb', line 31

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

#pathString

Return the normalized path to this node.

Returns:

  • (String)

    the normalized path.



23
24
25
26
# File 'lib/json_p3/node.rb', line 23

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

#to_sObject



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

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