Class: Checkoff::TaskSelectors

Inherits:
Object
  • Object
show all
Defined in:
lib/checkoff/task_selectors.rb,
sig/checkoff.rbs

Overview

Filter lists of tasks using declarative selectors.

Constant Summary collapse

MINUTE =

Returns:

  • (Object)
60
HOUR =

Returns:

  • (Object)
MINUTE * 60
DAY =

Returns:

  • (Object)
24 * HOUR
REALLY_LONG_CACHE_TIME =

Returns:

  • (Object)
HOUR
LONG_CACHE_TIME =

Returns:

  • (Object)
MINUTE * 15
SHORT_CACHE_TIME =

Returns:

  • (Object)
MINUTE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: Checkoff::Internal::ConfigLoader.load(:asana), client: Checkoff::Clients.new(config:).client, tasks: Checkoff::Tasks.new(config:, client:), timelines: Checkoff::Timelines.new(config:, client:), custom_fields: Checkoff::CustomFields.new(config:, client:)) ⇒ Object

sord warn - Asana::Client wasn't able to be resolved to a constant in this project @param config

@param client

@param tasks

@param timelines

@param custom_fields



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/checkoff/task_selectors.rb', line 28

def initialize(config: Checkoff::Internal::ConfigLoader.load(:asana),
               client: Checkoff::Clients.new(config:).client,
               tasks: Checkoff::Tasks.new(config:,
                                          client:),
               timelines: Checkoff::Timelines.new(config:,
                                                  client:),
               custom_fields: Checkoff::CustomFields.new(config:,
                                                         client:))
  @config = config
  @tasks = tasks
  @timelines = timelines
  @custom_fields = custom_fields
end

Instance Attribute Details

#custom_fieldsCheckoff::CustomFields (readonly)



61
62
63
# File 'lib/checkoff/task_selectors.rb', line 61

def custom_fields
  @custom_fields
end

#tasksCheckoff::Tasks (readonly)

Returns:



55
56
57
# File 'lib/checkoff/task_selectors.rb', line 55

def tasks
  @tasks
end

#timelinesCheckoff::Timelines (readonly)

Returns:



58
59
60
# File 'lib/checkoff/task_selectors.rb', line 58

def timelines
  @timelines
end

Class Method Details

.project_nameString

Returns:

  • (String)


91
92
93
# File 'lib/checkoff/task_selectors.rb', line 91

def project_name
  ARGV[1] || raise('Please pass project name to pull tasks from as first argument')
end

.runvoid

This method returns an undefined value.



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/checkoff/task_selectors.rb', line 101

def run
  require 'checkoff/projects'

  task_selectors = Checkoff::TaskSelectors.new
  extra_fields = ['custom_fields']
  projects = Checkoff::Projects.new
  project = projects.project_or_raise(workspace_name, project_name)
  raw_tasks = projects.tasks_from_project(project, extra_fields:)
  tasks = raw_tasks.filter { |task| task_selectors.filter_via_task_selector(task, task_selector) }
  # avoid n+1 queries generating the full task formatting
  puts JSON.pretty_generate(tasks.map(&:to_h))
end

.task_selectorArray(Symbol, Array)

Returns:

  • (Array(Symbol, Array))


65
66
67
68
69
# File 'lib/checkoff/task_selectors.rb', line 65

def task_selector
  task_selector_json = ARGV[2] || raise('Please pass task_selector in JSON form as third argument')
  function_name, args = validate_task_selector(JSON.parse(task_selector_json))
  [function_name.to_sym, args]
end

.validate_task_selector(parsed) ⇒ [String, ::Array[untyped]]

@param parsed — result of JSON.parse on a task selector

Parameters:

  • parsed (Object)

Returns:

  • ([String, ::Array[untyped]])


73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/checkoff/task_selectors.rb', line 73

def validate_task_selector(parsed)
  raise "Expected a 2-element [function_name, args] array, got #{parsed.inspect}" \
    unless parsed.is_a?(Array) && parsed.length == 2

  function_name = parsed.fetch(0)
  args = parsed.fetch(1)
  raise "Expected function_name to be a String, got #{function_name.inspect}" \
    unless function_name.is_a?(String)
  raise "Expected args to be an Array, got #{args.inspect}" unless args.is_a?(Array)

  [function_name, args]
end

.workspace_nameString

Returns:

  • (String)


96
97
98
# File 'lib/checkoff/task_selectors.rb', line 96

def workspace_name
  ARGV[0] || raise('Please pass workspace name as first argument')
end

Instance Method Details

#filter_via_task_selector(task, task_selector) ⇒ Boolean

sord warn - Asana::Resources::Task wasn't able to be resolved to a constant in this project @param task

@param task_selector — Filter based on task details. Examples: [:tag, 'foo'] [:not, [:tag, 'foo']] [:tag, 'foo']

Parameters:

  • task (Asana::Resources::Task)
  • task_selector (Symbol, ::Array[(Symbol | String | Integer | ::Array[untyped])])

Returns:

  • (Boolean)


46
47
48
49
50
# File 'lib/checkoff/task_selectors.rb', line 46

def filter_via_task_selector(task, task_selector)
  evaluator = TaskSelectorEvaluator.new(task:, tasks:, timelines:,
                                        custom_fields:)
  !!evaluator.evaluate(task_selector)
end