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.201.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.200.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.199.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.198.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.197.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.196.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.195.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.194.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.193.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.192.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.191.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.190.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.189.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.188.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.187.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.186.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.185.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.184.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.183.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.182.0 lib/checkoff/internal/selector_evaluator.rb