Module: Jamespath

Defined in:
lib/jamespath.rb,
lib/jamespath/vm.rb,
lib/jamespath/parser.rb,
lib/jamespath/version.rb,
lib/jamespath/tokenizer.rb

Overview

Defined Under Namespace

Classes: Parser, Token, Tokenizer, VM

Constant Summary collapse

VERSION =
'0.5.1'

Class Method Summary collapse

Class Method Details

.compile(query) ⇒ VM

Compiles an expression that can be searched.

Examples:

Compiling an expression

expr = Jamespath.compile('foo.bar')
expr.search(foo: {bar: 'result1'}) #=> 'result1'
expr.search(foo: {bar: 'result2'}) #=> 'result2'

Parameters:

  • query (String)

    the expression to search for.

Returns:

  • (VM)

    a virtual machine object that can interpret the expression.

See Also:



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

def compile(query)
  VM.new(Parser.new.parse(query))
end

.search(query, object) ⇒ Object?

Searches an object with a given JMESpath expression.

Examples:

Searching an object

Jamespath.search('foo.bar', foo: {bar: 'result'}) #=> 'result'

Parameters:

  • query (String)

    the expression to search for.

  • object (Object)

    an object to search for the expression in.

Returns:

  • (Object)

    the object, or list of objects, that match the expression.

  • (nil)

    if no objects matched the expression



18
19
20
# File 'lib/jamespath.rb', line 18

def search(query, object)
  compile(query).search(object)
end