Class: Janeway::Interpreters::Yielder

Inherits:
Object
  • Object
show all
Includes:
IterationHelper
Defined in:
lib/janeway/interpreters/yielder.rb

Overview

Yields each input value.

This is inserted at the end of the "real" selectors in the AST, to receive and yield the output. This is a supporting class for the Janeway.each method.

Constant Summary collapse

EMPTY_RESULT =

Frozen empty array — Yielder is only used by Enumerator#each and #find_paths, both of which discard the traversal's return value (results are surfaced via yield instead). Returning a shared frozen array avoids allocating a wrapper array per leaf.

[].freeze

Constants included from IterationHelper

IterationHelper::PASS_ALL

Instance Method Summary collapse

Methods included from IterationHelper

#make_yield_proc, #normalized_path

Constructor Details

#initialize(&block) ⇒ Yielder

Returns a new instance of Yielder.



15
16
17
18
19
20
# File 'lib/janeway/interpreters/yielder.rb', line 15

def initialize(&block)
  @block = block

  # Make a proc that yields the correct number of values to a block
  @yield_proc = make_yield_proc(&block)
end

Instance Method Details

#as_jsonHash

Returns:

  • (Hash)


47
48
49
# File 'lib/janeway/interpreters/yielder.rb', line 47

def as_json
  { type: self.class.to_s.split('::').last }
end

#interpret(input, parent, _root, path) {|matched| ... } ⇒ Array

Yield each input value

Parameters:

  • input (Array, Hash)

    the results of processing so far

  • parent (Array, Hash)

    parent of the input object

  • _root (Array, Hash)

    the entire input

  • path (Array<String, Integer>)

    components of normalized path to the current input

Yield Parameters:

  • matched (Object)

    value

Returns:

  • (Array)

    frozen empty array — see EMPTY_RESULT



36
37
38
39
# File 'lib/janeway/interpreters/yielder.rb', line 36

def interpret(input, parent, _root, path)
  @yield_proc.call(input, parent, path)
  EMPTY_RESULT
end

#next=void

This method returns an undefined value.

Dummy method from Interpreters::Base, allow child segment interpreter to disable the non-exist 'next' link.



44
# File 'lib/janeway/interpreters/yielder.rb', line 44

def next=(*); end