Class: JSONP3::Path::Environment

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

Overview

JSONPath configuration

Configure an environment by inheriting from Environment and setting one or more constants and/or overriding #setup_function_extensions.

Constant Summary collapse

MAX_INT_INDEX =

The maximum integer allowed when selecting array items by index.

(2**53) - 1
MIN_INT_INDEX =

The minimum integer allowed when selecting array items by index.

-(2**53) + 1
MAX_RECURSION_DEPTH =

The maximum number of arrays and hashes the descendent segment will traverse before raising a JSONPathRecursionError.

100
NAME_SELECTOR =

One of the available implementations of the name selector.

  • NameSelector (the default) will select values from hashes using string keys.
  • SymbolNameSelector will select values from hashes using string or symbol keys.

Implement your own name selector by inheriting from NameSelector and overriding #resolve.

NameSelector
INDEX_SELECTOR =

An implementation of the index selector. The default implementation will select values from arrays only. Implement your own by inheriting from IndexSelector and overriding #resolve.

IndexSelector

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEnvironment

Returns a new instance of Environment.



46
47
48
49
# File 'lib/json_p3/path/environment.rb', line 46

def initialize
  @function_extensions = {}
  setup_function_extensions
end

Instance Attribute Details

#function_extensionsObject

Returns the value of attribute function_extensions.



44
45
46
# File 'lib/json_p3/path/environment.rb', line 44

def function_extensions
  @function_extensions
end

Instance Method Details

#compile(query) ⇒ Query

Prepare JSONPath expression query for repeated application.

Parameters:

  • query (String)

Returns:



54
55
56
57
# File 'lib/json_p3/path/environment.rb', line 54

def compile(query)
  tokens = JSONP3::Path.tokenize(query)
  Query.new(self, Parser.new(self, query, tokens).parse)
end

#find(query, value) ⇒ Array<JSONPath>

Apply JSONPath expression query to value.

Parameters:

  • query (String)

    the JSONPath expression

  • value (JSON-like data)

    the target JSON "document"

Returns:

  • (Array<JSONPath>)


63
64
65
# File 'lib/json_p3/path/environment.rb', line 63

def find(query, value)
  compile(query).find(value)
end

#find_enum(query, value) ⇒ Enumerable<JSONPath>

Apply JSONPath expression query to value.

Parameters:

  • query (String)

    the JSONPath expression

  • value (JSON-like data)

    the target JSON "document"

Returns:

  • (Enumerable<JSONPath>)


71
72
73
# File 'lib/json_p3/path/environment.rb', line 71

def find_enum(query, value)
  compile(query).find_enum(value)
end

#first(path, value) ⇒ JSONPathNode | nil

Apply JSONPath expression query to value an return the first available node, or nil if there were no matches.

Parameters:

  • query (String)

    the JSONPath expression

  • value (JSON-like data)

    the target JSON "document"

Returns:

  • (JSONPathNode | nil)


98
99
100
# File 'lib/json_p3/path/environment.rb', line 98

def first(path, value)
  find_enum(path, value).first
end

#match(path, value) ⇒ JSONPathNode | nil

Apply JSONPath expression query to value an return the first available node.

Parameters:

  • query (String)

    the JSONPath expression

  • value (JSON-like data)

    the target JSON "document"

Returns:

  • (JSONPathNode | nil)


80
81
82
# File 'lib/json_p3/path/environment.rb', line 80

def match(path, value)
  find_enum(path, value).first
end

#match?(path, value) ⇒ bool

Apply JSONPath expression query to value an return true if there's at least one node, or nil if there were no matches.

Parameters:

  • query (String)

    the JSONPath expression

  • value (JSON-like data)

    the target JSON "document"

Returns:

  • (bool)


89
90
91
# File 'lib/json_p3/path/environment.rb', line 89

def match?(path, value)
  !find_enum(path, value).first.nil?
end

#setup_function_extensionsObject

Override this function to configure JSONPath function extensions. By default, only the standard functions described in RFC 9535 are enabled.



104
105
106
107
108
109
110
# File 'lib/json_p3/path/environment.rb', line 104

def setup_function_extensions
  @function_extensions["length"] = Length.new
  @function_extensions["count"] = Count.new
  @function_extensions["value"] = Value.new
  @function_extensions["match"] = Match.new
  @function_extensions["search"] = Search.new
end