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:), sections: Checkoff::Sections.new(config:), logger: $stderr) ⇒ void

Parameters:

  • from_workspace_arg (Symbol, String)
  • from_project_arg (Symbol, String)
  • from_section_arg (nil, String)
  • to_workspace_arg (Symbol, String)
  • to_project_arg (Symbol, String)
  • to_section_arg (Symbol, String, nil)

    :source_section

  • config (Checkoff::Internal::EnvFallbackConfigLoader, Hash) (defaults to: Checkoff::Internal::ConfigLoader.load(:asana))
  • projects (Checkoff::Projects) (defaults to: Checkoff::Projects.new(config:))
  • sections (Checkoff::Sections) (defaults to: Checkoff::Sections.new(config:))
  • logger (IO) (defaults to: $stderr)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/checkoff/cli.rb', line 83

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:),
               sections: Checkoff::Sections.new(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) ⇒ String, Symbol

Parameters:

  • to_project_arg (Symbol, String)

Returns:

  • (String, Symbol)


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

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) ⇒ nil, ...

Parameters:

  • to_section_arg (Symbol, String, nil)

    :source_section

Returns:

  • (nil, String, Symbol)


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

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) ⇒ Enumerable<Asana::Resources::Task>

Parameters:

  • from_workspace_name (String)
  • from_project_name (String, Symbol)
  • from_section_name (String, Symbol)

Returns:

  • (Enumerable<Asana::Resources::Task>)


118
119
120
121
122
123
124
# File 'lib/checkoff/cli.rb', line 118

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) ⇒ void

This method returns an undefined value.

Parameters:

  • tasks (Enumerable<Asana::Resources::Task>)
  • to_project (Asana::Resources::Project)
  • to_section (Asana::Resources::Section)


105
106
107
108
109
110
111
112
# File 'lib/checkoff/cli.rb', line 105

def move_tasks(tasks, to_project, to_section)
  tasks.each do |task|
    # a. check if already in correct project and section (future)
    # 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

#runvoid

This method returns an undefined value.



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/checkoff/cli.rb', line 127

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) ⇒ void

This method returns an undefined value.

Parameters:

  • from_workspace_arg (Symbol, String)
  • from_project_arg (Symbol, String)
  • from_section_arg (nil, String)


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

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) ⇒ void

This method returns an undefined value.

Parameters:

  • to_workspace_arg (Symbol, String)
  • to_project_arg (Symbol, String)
  • to_section_arg (Symbol, String, nil)

    :source_section

Raises:

  • (NotImplementedError)


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

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