Class: Enumpath::Operator::Union

Inherits:
Base
  • Object
show all
Defined in:
lib/enumpath/operator/union.rb

Overview

Implements JSONPath union operator syntax. See README for syntax and examples

Constant Summary collapse

OPERATOR =
/,/
CONSTRAINTS =
/^,|,$/
SPLIT_REGEX =
/,/

Instance Attribute Summary

Attributes inherited from Base

#operator

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Enumpath::Operator::Base

Class Method Details

.detect?(operator) ⇒ true, false

Whether the operator matches OPERATOR and does not match CONSTRAINTS

Parameters:

  • operator (String)

    the the full, normalized operator to test

Returns:

  • (true, false)

    whether the operator param appears to represent the operator class



23
24
25
# File 'lib/enumpath/operator/union.rb', line 23

def detect?(operator)
  operator.scan(',').any? && operator.scan(CONSTRAINTS).none?
end

Instance Method Details

#apply(remaining_path, enum, resolved_path) {|remaining_path, enum, resolved_path| ... } ⇒ Object

Yields to the block once for every union member

Parameters:

  • remaining_path (Array)

    an array containing the normalized path segments yet to be resolved

  • enum (Enumerable)

    the object to apply the operator to

  • resolved_path (Array)

    an array containing the static path segments that have been resolved

Yields:

  • A block that will be called if the operator is applied successfully. If the operator cannot or should not be applied then the block is not yielded.

Yield Parameters:

  • remaining_path (Array)

    the union member plus remaining_path

  • enum (Enumerable)

    enum

  • resolved_path (Array)

    resolved_path



35
36
37
38
39
# File 'lib/enumpath/operator/union.rb', line 35

def apply(remaining_path, enum, resolved_path)
  parts = operator.split(SPLIT_REGEX).map { |part| part.strip.gsub(/^['"]|['"]$/, '') }
  Enumpath.log('Applying union parts') { { parts: parts } }
  parts.each { |part| yield([part] + remaining_path, enum, resolved_path) }
end