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.

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`.

## Optimizing

By default the runtime will perform optimizations on the expression to try to make it run searches as fast as possible. If all your searches use different expressions this might not be worth the extra work, so you can disable the optimizer by passing ‘:optimize_expression => false`. If you disable caching it is also recommended that you disable optimizations, but you don’t have to The optimizer will be disabled if you pass a custom parser with the ‘:parser` option.

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`.

  • :optimize_expressions (Boolean) — default: true

    When ‘false`, no additional optimizations will be performed on the expression, when `true` the expression will be analyzed and optimized. This increases the time it takes to parse, but improves the speed of searches, so it’s highly recommended if you’re using the same expression multiple times and have not disabled caching. 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)


59
60
61
# File 'lib/jmespath/runtime.rb', line 59

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

Instance Attribute Details

#parserParser (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:



64
65
66
# File 'lib/jmespath/runtime.rb', line 64

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)


69
70
71
# File 'lib/jmespath/runtime.rb', line 69

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