Class: Janeway::Enumerator

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/janeway/enumerator.rb

Overview

Enumerator combines a parsed JSONpath query with input. It provides enumerator methods.

Instance Method Summary collapse

Constructor Details

#initialize(query, input) ⇒ Enumerator

Returns a new instance of Enumerator.

Parameters:

Raises:

  • (ArgumentError)


10
11
12
13
14
15
# File 'lib/janeway/enumerator.rb', line 10

def initialize(query, input)
  @query = query
  @input = input

  raise ArgumentError, "expect Janeway::Query, got #{query.inspect}" unless query.is_a?(Query)
end

Instance Method Details

#deleteArray, Hash

Delete each value matched by the JSONPath query.

Returns:

  • (Array, Hash)


39
40
41
# File 'lib/janeway/enumerator.rb', line 39

def delete
  Janeway::Interpreter.new(@query, as: :deleter).interpret(@input)
end

#each {|value, parent, hash, normalized| ... } ⇒ void

This method returns an undefined value.

Iterate through each value matched by the JSONPath query.

Yield Parameters:

  • value (Object)

    matched by query

  • parent (Array, Hash)

    object that contains the value

  • hash (String, Integer)

    key or array index of the value within the parent object

  • normalized (String)

    jsonpath that uniqely points to this value



31
32
33
34
35
# File 'lib/janeway/enumerator.rb', line 31

def each(&block)
  return enum_for(:each) unless block_given?

  Janeway::Interpreter.new(@query, as: :iterator, &block).interpret(@input)
end

#searchArray

Return a list of values from the input data that match the jsonpath query

Returns:

  • (Array)

    all matched objects



20
21
22
# File 'lib/janeway/enumerator.rb', line 20

def search
  Janeway::Interpreter.new(@query).interpret(@input)
end