Class: JsonPointer::Evaluator

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

Overview

Evaluates a JSON pointer within a JSON document.

Note that this class is designed to evaluate references across a plain JSON data object or an instance of ‘JsonSchema::Schema`, so the constructor’s ‘data` argument can be of either type.

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Evaluator

Returns a new instance of Evaluator.



8
9
10
# File 'lib/json_pointer/evaluator.rb', line 8

def initialize(data)
  @data = data
end

Instance Method Details

#evaluate(original_path) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/json_pointer/evaluator.rb', line 12

def evaluate(original_path)
  path = original_path

  # the leading # can either be included or not
  path = path[1..-1] if path[0] == "#"

  # special case on "" or presumably "#"
  if path.empty?
    return @data
  end

  if path[0] != "/"
    raise ArgumentError, %{Path must begin with a leading "/": #{original_path}.}
  end

  path_parts = split(path)
  evaluate_segment(@data, path_parts)
end