Module: Janeway::Interpreters::IterationHelper
- Included in:
- ArraySliceSelectorDeleteIf, FilterSelectorDeleteIf, IndexSelectorDeleteIf, NameSelectorDeleteIf, WildcardSelectorDeleteIf, Yielder
- Defined in:
- lib/janeway/interpreters/iteration_helper.rb
Overview
Mixin for interpreter classes that yield to a block
Instance Method Summary collapse
-
#make_yield_proc(&block) ⇒ Proc
Returns a Proc that yields the correct number of parameters to a block.
-
#normalized_path(components) ⇒ String
Convert the list of path elements into a normalized query string.
Instance Method Details
#make_yield_proc(&block) ⇒ Proc
Returns a Proc that yields the correct number of parameters to a block
block.arity is -1 when no block is given, and an enumerator is being returned
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/janeway/interpreters/iteration_helper.rb', line 13 def make_yield_proc(&block) if block.arity.negative? # Yield just the value to an enumerator, to enable instance method calls on # matched values like this: enum.delete_if(&:even?) 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 parameters regardless. proc { |value, parent, path| @block.call(value, parent, path.last) } end 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.
37 38 39 40 41 42 |
# File 'lib/janeway/interpreters/iteration_helper.rb', line 37 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 |