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.115.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.114.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.113.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.112.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.111.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.110.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.109.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.108.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.107.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.106.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.105.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.104.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.103.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.102.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.101.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.100.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.99.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.98.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.97.0 lib/checkoff/internal/selector_evaluator.rb
checkoff-0.96.0 lib/checkoff/internal/selector_evaluator.rb