Class: Checkoff::Sections

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/checkoff/sections.rb

Overview

Query different sections of Asana projects

Constant Summary collapse

MINUTE =
60
LONG_CACHE_TIME =
MINUTE * 15
SHORT_CACHE_TIME =
MINUTE * 5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(projects: Checkoff::Projects.new, time: Time) ⇒ Sections

Returns a new instance of Sections.



16
17
18
19
20
# File 'lib/checkoff/sections.rb', line 16

def initialize(projects: Checkoff::Projects.new,
               time: Time)
  @projects = projects
  @time = time
end

Instance Attribute Details

#projectsObject (readonly)

Returns the value of attribute projects.



14
15
16
# File 'lib/checkoff/sections.rb', line 14

def projects
  @projects
end

#timeObject (readonly)

Returns the value of attribute time.



14
15
16
# File 'lib/checkoff/sections.rb', line 14

def time
  @time
end

Instance Method Details

#by_section(tasks) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/checkoff/sections.rb', line 35

def by_section(tasks)
  current_section = nil
  by_section = {}
  tasks.each do |task|
    current_section, by_section = file_task_by_section(current_section,
                                                       by_section, task)
  end
  by_section
end

#file_task_by_section(current_section, by_section, task) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/checkoff/sections.rb', line 24

def file_task_by_section(current_section, by_section, task)
  if task.name =~ /:$/
    current_section = task.name
    by_section[current_section] = []
  else
    by_section[current_section] ||= []
    by_section[current_section] << task
  end
  [current_section, by_section]
end

#project_or_raise(workspace_name, project_name) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/checkoff/sections.rb', line 62

def project_or_raise(workspace_name, project_name)
  project = projects.project(workspace_name, project_name)
  if project.nil?
    raise "Could not find project #{project_name} " \
          "under workspace #{workspace_name}"
  end
  project
end

#project_task_names(workspace_name, project_name) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/checkoff/sections.rb', line 119

def project_task_names(workspace_name, project_name)
  by_section = tasks_by_section(workspace_name, project_name)
  by_section.flat_map do |section_name, tasks|
    task_names = tasks.map(&:name)
    if section_name.nil?
      task_names
    else
      [section_name, task_names]
    end
  end
end

#raw_subtasks(task) ⇒ Object

Returns all subtasks, including section headers



104
105
106
# File 'lib/checkoff/sections.rb', line 104

def raw_subtasks(task)
  task.subtasks(projects.task_options)
end

#section_task_names(workspace_name, project_name, section_name) ⇒ Object



91
92
93
94
95
96
97
98
99
100
# File 'lib/checkoff/sections.rb', line 91

def section_task_names(workspace_name, project_name, section_name)
  tasks = tasks(workspace_name, project_name, section_name)
  if tasks.nil?
    by_section = tasks_by_section(workspace_name, project_name)
    desc = "#{workspace_name} | #{project_name} | #{section_name}"
    raise "Could not find task names for #{desc}.  " \
          "Valid sections: #{by_section.keys}"
  end
  tasks.map(&:name)
end

#task_due?(task) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
112
113
114
115
116
117
# File 'lib/checkoff/sections.rb', line 109

def task_due?(task)
  if task.due_at
    Time.parse(task.due_at) <= time.now
  elsif task.due_on
    Date.parse(task.due_on) <= time.today
  else
    true # set a due date if you don't want to do this now
  end
end

#tasks(workspace_name, project_name, section_name) ⇒ Object

XXX: Rename to section_tasks



86
87
88
# File 'lib/checkoff/sections.rb', line 86

def tasks(workspace_name, project_name, section_name)
  tasks_by_section(workspace_name, project_name)[section_name]
end

#tasks_by_section(workspace_name, project_name) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/checkoff/sections.rb', line 71

def tasks_by_section(workspace_name, project_name)
  project = project_or_raise(workspace_name, project_name)
  if project_name == :my_tasks_new
    tasks_by_section_for_project_and_assignee_status(project, 'inbox')
  elsif project_name == :my_tasks_today
    tasks_by_section_for_project_and_assignee_status(project, 'today')
  elsif project_name == :my_tasks_upcoming
    tasks_by_section_for_project_and_assignee_status(project, 'upcoming')
  else
    tasks_by_section_for_project(project)
  end
end

#tasks_by_section_for_project(project) ⇒ Object



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

def tasks_by_section_for_project(project)
  raw_tasks = projects.tasks_from_project(project)
  active_tasks = projects.active_tasks(raw_tasks)
  by_section(active_tasks)
end

#tasks_by_section_for_project_and_assignee_status(project, assignee_status) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/checkoff/sections.rb', line 52

def tasks_by_section_for_project_and_assignee_status(project,
                                                     assignee_status)
  raw_tasks = projects.tasks_from_project(project)
  by_assignee_status =
    projects.active_tasks(raw_tasks)
      .group_by(&:assignee_status)
  active_tasks = by_assignee_status[assignee_status]
  by_section(active_tasks)
end