class Ruby::Pomodoro::Tasks::Editor

Attributes

editor[R]
file_path[R]
tasks_repo[R]

Public Class Methods

new(tasks_repo: Resource, file_path: Ruby::Pomodoro.tasks_file_path, editor: TTY::Editor) click to toggle source

@param [Array<Ruby::Pomodoro::Task>] tasks_repo @param [String] file_path @param editor [Class, Object] Editor with method open, for create and open file

# File lib/ruby/pomodoro/tasks/editor.rb, line 10
def initialize(tasks_repo: Resource, file_path: Ruby::Pomodoro.tasks_file_path, editor: TTY::Editor)
  @tasks_repo = tasks_repo
  @file_path = file_path
  @editor = editor
end

Public Instance Methods

edit() click to toggle source

Open editor and save tasks from tmp file to task_repo @return [TrueClass]

# File lib/ruby/pomodoro/tasks/editor.rb, line 18
def edit
  save
  editor.open(file_path)
  create_tasks
  true
end
load() click to toggle source

Load tasks form file @return [TrueClass]

# File lib/ruby/pomodoro/tasks/editor.rb, line 38
def load
  create_tasks
  true
end
save() click to toggle source

save list to disc @return [TrueClass]

# File lib/ruby/pomodoro/tasks/editor.rb, line 27
def save
  File.open(file_path, "w") do |f|
    if @tasks_repo.size > 0
      f.puts(@tasks_repo.all.map {|task| print_task(task) }.join("\n"))
    end
  end
  true
end

Private Instance Methods

create_tasks() click to toggle source
# File lib/ruby/pomodoro/tasks/editor.rb, line 49
def create_tasks
  tasks_repo.delete_all
  read_tmp_file.each do |line|
    next unless line

    name, time = line.split("|").compact.map {|l| l.chomp.strip }
    tasks_repo.create(name: name, spent_time: TimeConverter.to_seconds(time))
  end
end
print_task(task) click to toggle source
read_tmp_file() click to toggle source
# File lib/ruby/pomodoro/tasks/editor.rb, line 45
def read_tmp_file
  File.readlines(file_path)
end