lib/checkoff/internal/task_selector_evaluator.rb in checkoff-0.41.0 vs lib/checkoff/internal/task_selector_evaluator.rb in checkoff-0.42.0
- old
+ new
@@ -1,11 +1,11 @@
# frozen_string_literal: true
module Checkoff
# Base class to evaluate a task selector function given fully evaluated arguments
class FunctionEvaluator
- # @param task_selector [Array<(Symbol, Array)>]
+ # @param task_selector [Array<(Symbol, Array)>,String]
# @param tasks [Checkoff::Tasks]
def initialize(task_selector:,
tasks:)
@task_selector = task_selector
@tasks = tasks
@@ -163,10 +163,25 @@
def evaluate(_task, subvalue)
subvalue.nil?
end
end
+ # :equals? function
+ class EqualsPFunctionEvaluator < FunctionEvaluator
+ def matches?
+ fn?(task_selector, :equals?)
+ end
+
+ # @param _task [Asana::Resources::Task]
+ # @param lhs [Object]
+ # @param rhs [Object]
+ # @return [Boolean]
+ def evaluate(_task, lhs, rhs)
+ lhs == rhs
+ end
+ end
+
# :tag function
class TagPFunctionEvaluator < FunctionEvaluator
def matches?
fn?(task_selector, :tag)
end
@@ -322,10 +337,24 @@
n_days_from_now = (Time.now + (num_days * 24 * 60 * 60))
time < n_days_from_now
end
end
+ # String literals
+ class StringLiteralEvaluator < FunctionEvaluator
+ def matches?
+ task_selector.is_a?(String)
+ end
+
+ # @sg-ignore
+ # @param _task [Asana::Resources::Task]
+ # @return [String]
+ def evaluate(_task)
+ task_selector
+ end
+ end
+
# Evaluator task selectors against a task
class TaskSelectorEvaluator
# @param task [Asana::Resources::Task]
# @param tasks [Checkoff::Tasks]
def initialize(task:,
@@ -335,19 +364,21 @@
end
FUNCTION_EVALUTORS = [
NotFunctionEvaluator,
NilPFunctionEvaluator,
+ EqualsPFunctionEvaluator,
TagPFunctionEvaluator,
CustomFieldValueFunctionEvaluator,
CustomFieldGidValueFunctionEvaluator,
CustomFieldGidValueContainsAnyGidFunctionEvaluator,
CustomFieldGidValueContainsAllGidsFunctionEvaluator,
AndFunctionEvaluator,
DuePFunctionEvaluator,
DueDateSetPFunctionEvaluator,
CustomFieldLessThanNDaysFromNowFunctionEvaluator,
+ StringLiteralEvaluator,
].freeze
# @param task_selector [Array]
# @return [Boolean, Object, nil]
def evaluate(task_selector)
@@ -373,16 +404,21 @@
# @sg-ignore
# @param task_selector [Array]
# @param evaluator [FunctionEvaluator]
# @return [Boolean, Object, nil]
def try_this_evaluator(task_selector, evaluator)
- evaluated_args = task_selector[1..].map.with_index do |item, index|
- if evaluator.evaluate_arg?(index)
- evaluate(item)
- else
- item
- end
- end
+ # if task_selector is an array
+ evaluated_args = if task_selector.is_a?(Array)
+ task_selector[1..].map.with_index do |item, index|
+ if evaluator.evaluate_arg?(index)
+ evaluate(item)
+ else
+ item
+ end
+ end
+ else
+ []
+ end
evaluator.evaluate(task, *evaluated_args)
end
# @return [Asana::Resources::Task]