Class: Janeway::Interpreters::Yielder
- Inherits:
-
Object
- Object
- Janeway::Interpreters::Yielder
- 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
- #as_json ⇒ Hash
-
#initialize(&block) ⇒ Yielder
constructor
A new instance of Yielder.
-
#interpret(input, parent, _root, path) {|matched| ... } ⇒ Object
Yield each input value.
-
#next= ⇒ void
Dummy method from Interpreters::Base, allow child segment interpreter to disable the non-exist ‘next’ link.
-
#normalized_path(components) ⇒ String
Convert the list of path elements into a normalized query string.
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_json ⇒ 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
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.
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 |