Class: Checkoff::ViewSubcommand

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

Overview

CLI subcommand that shows tasks in JSON form

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(workspace_name, project_name, section_name, task_name, config: Checkoff::Internal::ConfigLoader.load(:asana), projects: Checkoff::Projects.new(config:), sections: Checkoff::Sections.new(config:, projects:), tasks: Checkoff::Tasks.new(config:, sections:), stderr: $stderr) ⇒ Object

@param workspace_name

@param project_name

@param section_name

@param task_name

@param config

@param projects

@param sections

@param tasks

@param stderr



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/checkoff/cli.rb', line 172

def initialize(workspace_name, project_name, section_name,
               task_name,
               config: Checkoff::Internal::ConfigLoader.load(:asana),
               projects: Checkoff::Projects.new(config:),
               sections: Checkoff::Sections.new(config:,
                                                projects:),
               tasks: Checkoff::Tasks.new(config:,
                                          sections:),
               stderr: $stderr)
  @workspace_name = workspace_name
  @stderr = stderr
  validate_and_assign_project_name(project_name)
  @section_name = section_name
  @task_name = task_name
  @sections = sections
  @tasks = tasks
end

Instance Attribute Details

#project_nameObject (readonly)

Returns the value of attribute project_name.

Returns:

  • (Object)


181
182
183
# File 'sig/checkoff.rbs', line 181

def project_name
  @project_name
end

#section_nameObject (readonly)

Returns the value of attribute section_name.

Returns:

  • (Object)


184
185
186
# File 'sig/checkoff.rbs', line 184

def section_name
  @section_name
end

#sectionsObject (readonly)

Returns the value of attribute sections.

Returns:

  • (Object)


190
191
192
# File 'sig/checkoff.rbs', line 190

def sections
  @sections
end

#stderrObject (readonly)

Returns the value of attribute stderr.

Returns:

  • (Object)


196
197
198
# File 'sig/checkoff.rbs', line 196

def stderr
  @stderr
end

#task_nameObject (readonly)

Returns the value of attribute task_name.

Returns:

  • (Object)


187
188
189
# File 'sig/checkoff.rbs', line 187

def task_name
  @task_name
end

#tasksObject (readonly)

Returns the value of attribute tasks.

Returns:

  • (Object)


193
194
195
# File 'sig/checkoff.rbs', line 193

def tasks
  @tasks
end

#workspace_nameObject (readonly)

Returns the value of attribute workspace_name.

Returns:

  • (Object)


178
179
180
# File 'sig/checkoff.rbs', line 178

def workspace_name
  @workspace_name
end

Instance Method Details

#runvoid

This method returns an undefined value.



191
192
193
194
195
196
197
198
199
200
# File 'lib/checkoff/cli.rb', line 191

def run
  if section_name.nil?
    run_on_project(workspace_name, project_name)
  elsif task_name.nil?
    run_on_section(workspace_name, project_name, section_name)
  else
    # @sg-ignore
    run_on_task(workspace_name, project_name, section_name, task_name)
  end
end

#run_on_project(workspace, project) ⇒ String

@param workspace

@param project

Parameters:

  • workspace (String, Symbol)
  • project (String, Symbol)

Returns:

  • (String)


219
220
221
222
223
224
225
226
227
# File 'lib/checkoff/cli.rb', line 219

def run_on_project(workspace, project)
  tasks_by_section =
    # @sg-ignore
    sections.tasks_by_section(workspace, project)
  tasks_by_section.update(tasks_by_section) do |_key, tasks|
    tasks_to_hash(tasks)
  end
  tasks_by_section.to_json
end

#run_on_section(workspace, project, section) ⇒ String

@param workspace

@param project

@param section

Parameters:

  • workspace (String, Symbol)
  • project (String, Symbol)
  • section (String, Symbol, nil)

Returns:

  • (String)


233
234
235
236
237
# File 'lib/checkoff/cli.rb', line 233

def run_on_section(workspace, project, section)
  section = nil if section == ''
  tasks = sections.tasks(workspace, project, section) || []
  tasks_to_hash(tasks).to_json
end

#run_on_task(workspace, project, section, task_name) ⇒ Object

@param workspace

@param project

@param section

@param task_name



244
245
246
247
248
249
250
251
# File 'lib/checkoff/cli.rb', line 244

def run_on_task(workspace, project, section, task_name)
  section = nil if section == ''
  task = tasks.task(workspace.to_s, project, task_name, section_name: section)
  raise "Task not found: #{task_name}" if task.nil?

  # @sg-ignore nil check above is not flow-sensitive
  task_to_hash(task).to_json
end

#task_to_hash(task) ⇒ ::Hash[Symbol, untyped]

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

Parameters:

  • task (Asana::Resources::Task)

Returns:

  • (::Hash[Symbol, untyped])


255
256
257
258
259
260
261
262
263
264
265
# File 'lib/checkoff/cli.rb', line 255

def task_to_hash(task)
  task_out = {
    name: task.name,
  }
  if task.due_on
    task_out[:due] = task.due_on
  elsif task.due_at
    task_out[:due] = task.due_at
  end
  task_out
end

#tasks_to_hash(tasks) ⇒ ::Array[::Hash[untyped, untyped]]

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

Parameters:

  • tasks (::Enumerable[Asana::Resources::Task])

Returns:

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


269
270
271
# File 'lib/checkoff/cli.rb', line 269

def tasks_to_hash(tasks)
  tasks.map { |task| task_to_hash(task) }
end

#validate_and_assign_project_name(project_name) ⇒ String, Symbol

@param project_name

Parameters:

  • project_name (String, Symbol)

Returns:

  • (String, Symbol)


206
207
208
209
210
211
212
213
# File 'lib/checkoff/cli.rb', line 206

def validate_and_assign_project_name(project_name)
  name = project_name.to_s
  @project_name = if name.start_with?(':')
                    name.delete_prefix(':').to_sym
                  else
                    name
                  end
end