Class: Janeway::Interpreters::Yielder

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

Overview

Yields each input value.

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

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Yielder

Returns a new instance of Yielder.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/janeway/interpreters/yielder.rb', line 13

def initialize(&block)
  @block = block

  # Decide how many parameters to yield to this block.
  # block.arity is -1 when no block was given, and an enumerator is being returned from #each
  @yield_to_block =
    if block.arity.negative?
      # Yield values only to an enumerator.
      proc { |value, _parent, _path| @block.call(value) }
    elsif block.arity > 3
      # Only do the work of constructing the normalized path when it is actually used
      proc { |value, parent, path| @block.call(value, parent, path.last, normalized_path(path)) }
    else
      # block arity is 1, 2 or 3. Send all 3.
      proc { |value, parent, path| @block.call(value, parent, path.last) }
    end
end

Instance Method Details

#as_jsonHash

Returns:

  • (Hash)


67
68
69
# File 'lib/janeway/interpreters/yielder.rb', line 67

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

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

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:

  • (Object)

    input as node list



39
40
41
42
# File 'lib/janeway/interpreters/yielder.rb', line 39

def interpret(input, parent, _root, path)
  @yield_to_block.call(input, parent, path)
  input.is_a?(Array) ? input : [input]
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.



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

def next=(*); end

#normalized_path(components) ⇒ String

Convert the list of path elements into a normalized query string.

This form uses a subset of jsonpath that unambiguously points to a value using only name and index selectors. Name selectors must use bracket notation, not shorthand.

Parameters:

  • components (Array<String, Integer>)

Returns:

  • (String)

See Also:



54
55
56
57
58
59
# File 'lib/janeway/interpreters/yielder.rb', line 54

def normalized_path(components)
  # First component is the root identifer, the remaining components are
  # all index selectors or name selectors.
  # Handle the root identifier separately, because .normalize does not handle those.
  '$' + components[1..].map { NormalizedPath.normalize(_1) }.join
end