Class: Checkoff::MvSubcommand

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

Overview

Move tasks from one place to another

Instance Method Summary collapse

Constructor Details

#initialize(from_workspace_arg:, from_project_arg:, from_section_arg:, to_workspace_arg:, to_project_arg:, to_section_arg:, config: Checkoff::Internal::ConfigLoader.load(:asana), projects: Checkoff::Projects.new(config: config), sections: Checkoff::Sections.new(config: config), logger: $stderr) ⇒ MvSubcommand

Returns a new instance of MvSubcommand.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/checkoff/cli.rb', line 58

def initialize(from_workspace_arg:,
               from_project_arg:,
               from_section_arg:,
               to_workspace_arg:,
               to_project_arg:,
               to_section_arg:,
               config: Checkoff::Internal::ConfigLoader.load(:asana),
               projects: Checkoff::Projects.new(config: config),
               sections: Checkoff::Sections.new(config: config),
               logger: $stderr)
  validate_and_assign_from_location(from_workspace_arg, from_project_arg, from_section_arg)
  validate_and_assign_to_location(to_workspace_arg, to_project_arg, to_section_arg)

  @projects = projects
  @sections = sections
  @logger = logger
end

Instance Method Details

#create_to_project_name(to_project_arg) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/checkoff/cli.rb', line 31

def create_to_project_name(to_project_arg)
  if to_project_arg == :source_project
    from_project_name
  else
    project_arg_to_name(to_project_arg)
  end
end

#create_to_section_name(to_section_arg) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/checkoff/cli.rb', line 39

def create_to_section_name(to_section_arg)
  if to_section_arg == :source_section
    from_section_name
  else
    to_section_arg
  end
end

#fetch_tasks(from_workspace_name, from_project_name, from_section_name) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/checkoff/cli.rb', line 85

def fetch_tasks(from_workspace_name, from_project_name, from_section_name)
  if from_section_name == :all_sections
    raise NotImplementedError, 'Not implemented: Teach me how to move all sections of a project'
  end

  sections.tasks(from_workspace_name, from_project_name, from_section_name)
end

#move_tasks(tasks, to_project, to_section) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/checkoff/cli.rb', line 76

def move_tasks(tasks, to_project, to_section)
  tasks.each do |task|
    # a. check if already in correct project and section (TODO)
    # b. if not, put it there
    @logger.puts "Moving #{task.name} to #{to_section.name}..."
    task.add_project(project: to_project.gid, section: to_section.gid)
  end
end

#runObject



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/checkoff/cli.rb', line 93

def run
  # 0. Look up project and section gids
  to_project = projects.project_or_raise(to_workspace_name, to_project_name)
  to_section = sections.section_or_raise(to_workspace_name, to_project_name, to_section_name)

  # 1. Get list of tasks which match
  tasks = fetch_tasks(from_workspace_name, from_project_name, from_section_name)
  # 2. for each task,
  move_tasks(tasks, to_project, to_section)
  # 3. tell the user we're done'
  @logger.puts 'Done moving tasks'
end

#validate_and_assign_from_location(from_workspace_arg, from_project_arg, from_section_arg) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/checkoff/cli.rb', line 17

def validate_and_assign_from_location(from_workspace_arg, from_project_arg, from_section_arg)
  if from_workspace_arg == :default_workspace
    # Figure out what to do here - we accept a default
    # workspace gid and default workspace_gid arguments elsewhere.
    # however, there are undefaulted workspace_name arguments as
    # well...
    raise NotImplementedError, 'Not implemented: Teach me how to look up default workspace name'
  end

  @from_workspace_name = from_workspace_arg
  @from_project_name = project_arg_to_name(from_project_arg)
  @from_section_name = from_section_arg
end

#validate_and_assign_to_location(to_workspace_arg, to_project_arg, to_section_arg) ⇒ Object

Raises:

  • (NotImplementedError)


47
48
49
50
51
52
53
54
55
56
# File 'lib/checkoff/cli.rb', line 47

def validate_and_assign_to_location(to_workspace_arg, to_project_arg, to_section_arg)
  @to_workspace_name = to_workspace_arg
  @to_workspace_name = from_workspace_name if to_workspace_arg == :source_workspace
  @to_project_name = create_to_project_name(to_project_arg)
  @to_section_name = create_to_section_name(to_section_arg)

  return unless from_workspace_name != to_workspace_name

  raise NotImplementedError, 'Not implemented: Teach me how to move tasks between workspaces'
end