Class: Checkoff::TaskSelectors

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

Overview

Filter lists of tasks using declarative selectors.

Constant Summary collapse

MINUTE =
60
HOUR =
MINUTE * 60
DAY =
24 * HOUR
REALLY_LONG_CACHE_TIME =
HOUR * 1
LONG_CACHE_TIME =
MINUTE * 15
SHORT_CACHE_TIME =
MINUTE

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: Checkoff::Internal::ConfigLoader.load(:asana), client: Checkoff::Clients.new(config: config).client, tasks: Checkoff::Tasks.new(config: config, client: client)) ⇒ TaskSelectors

Returns a new instance of TaskSelectors.



23
24
25
26
27
28
29
# File 'lib/checkoff/task_selectors.rb', line 23

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

Class Method Details

.project_nameObject



45
46
47
# File 'lib/checkoff/task_selectors.rb', line 45

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

.runObject



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/checkoff/task_selectors.rb', line 58

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: 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_selectorObject



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

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

.workspace_nameObject



49
50
51
# File 'lib/checkoff/task_selectors.rb', line 49

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

Instance Method Details

#filter_via_task_selector(task, task_selector) ⇒ Object

Parameters:

  • task_selector (Hash<Symbol, Object>)

    Filter based on description. Examples: ‘foo’ {tag: ‘foo’ (:tag ‘foo’)



33
34
35
36
# File 'lib/checkoff/task_selectors.rb', line 33

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