Class: JMESPath::Runtime Private

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

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Constant Summary collapse

DEFAULT_PARSER =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

CachingParser

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Runtime

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Constructs a new runtime object for evaluating JMESPath expressions.

runtime = JMESPath::Runtime.new
runtime.search(expression, data)
#=> ...

## Caching

When constructing a JMESPath::Runtime, the default parser caches expressions. This significantly speeds up calls to #search multiple times with the same expression but different data. To disable caching, pass `:cache_expressions => false` to the constructor or pass a custom `:parser`.

Examples:

Re-use a Runtime, caching enabled by default


runtime = JMESPath::Runtime.new
runtime.parser
#=> #<JMESPath::CachingParser ...>

Disable caching


runtime = JMESPath::Runtime.new(cache_expressions: false)
runtime.parser
#=> #<JMESPath::Parser ...>

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :cache_expressions (Boolean) — default: true

    When `false`, a non caching parser will be used. When `true`, a shared instance of CachingParser is used. Defaults to `true`.

  • :disable_visit_errors (Boolean) — default: false

    When `true`, no errors will be raised during runtime processing. Parse errors will still be raised, but unexpected data sent to visit will result in nil being returned.

  • :parser (Parser, CachingParser)


45
46
47
# File 'lib/jmespath/runtime.rb', line 45

def initialize(options = {})
  @parser = options[:parser] || default_parser(options)
end

Instance Attribute Details

#parserParser, CachingParser (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



50
51
52
# File 'lib/jmespath/runtime.rb', line 50

def parser
  @parser
end

Instance Method Details

#search(expression, data) ⇒ Mixed?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • expression (String<JMESPath>)
  • data (Hash)

Returns:

  • (Mixed, nil)


55
56
57
58
# File 'lib/jmespath/runtime.rb', line 55

def search(expression, data)
  optimized_expression = @parser.parse(expression).optimize
  optimized_expression.visit(data)
end