Sha256: 2d69ef9e3dc8997a8ab0caa68b43d0424940190d0e3dc4b9cba962cc88b3c052

Contents?: true

Size: 1.66 KB

Versions: 128

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

module Checkoff
  # Base class to evaluate Asana resource selectors against an Asana resource
  class SelectorEvaluator
    # @param selector [Array]
    # @return [Boolean, Object, nil]
    def evaluate(selector)
      return true if selector.empty?

      function_evaluators.each do |evaluator_class|
        # @type [SelectorClasses::FunctionEvaluator]
        # @sg-ignore
        evaluator = evaluator_class.new(selector: selector,
                                        **initializer_kwargs)

        next unless evaluator.matches?

        return try_this_evaluator(selector, evaluator)
      end

      raise "Syntax issue trying to handle #{selector.inspect}"
    end

    private

    # @return [Hash]
    def initializer_kwargs
      {}
    end

    # @return [Array<Class<FunctionEvaluator>>]
    # @sg-ignore
    def function_evaluators
      raise 'Implement me!'
    end

    # @param selector [Array]
    # @param evaluator [SelectorClasses::FunctionEvaluator]
    # @return [Array]
    def evaluate_args(selector, evaluator)
      return [] unless selector.is_a?(Array)

      selector[1..].map.with_index do |item, index|
        if evaluator.evaluate_arg?(index)
          evaluate(item)
        else
          item
        end
      end
    end

    # @param selector [Array]
    # @param evaluator [SelectorClasses::FunctionEvaluator]
    # @return [Boolean, Object, nil]
    def try_this_evaluator(selector, evaluator)
      # if selector is an array
      evaluated_args = evaluate_args(selector, evaluator)

      evaluator.evaluate(item, *evaluated_args)
    end

    # @return [Asana::Resources::Resource]
    attr_reader :item
  end
end

Version data entries

128 entries across 128 versions & 1 rubygems

Version Path
checkoff-0.95.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.94.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.93.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.92.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.91.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.90.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.89.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.88.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.87.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.86.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.85.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.84.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.83.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.82.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.69.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.68.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.67.1 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.67.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.66.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.65.0 lib/checkoff/internal/selector_evaluator.rb