Class: Ruby::Pomodoro::Tasks::Editor

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby/pomodoro/tasks/editor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tasks_repo: Resource, file_path: Ruby::Pomodoro.tasks_file_path, editor: TTY::Editor) ⇒ Editor

Returns a new instance of Editor.

Parameters:

  • tasks_repo (Array<Ruby::Pomodoro::Task>) (defaults to: Resource)
  • file_path (String) (defaults to: Ruby::Pomodoro.tasks_file_path)
  • editor (Class, Object) (defaults to: TTY::Editor)

    Editor with method open, for create and open file



10
11
12
13
14
# 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

Instance Attribute Details

#editorObject (readonly)

Returns the value of attribute editor.



5
6
7
# File 'lib/ruby/pomodoro/tasks/editor.rb', line 5

def editor
  @editor
end

#file_pathObject (readonly)

Returns the value of attribute file_path.



5
6
7
# File 'lib/ruby/pomodoro/tasks/editor.rb', line 5

def file_path
  @file_path
end

#tasks_repoObject (readonly)

Returns the value of attribute tasks_repo.



5
6
7
# File 'lib/ruby/pomodoro/tasks/editor.rb', line 5

def tasks_repo
  @tasks_repo
end

Instance Method Details

#editTrueClass

Open editor and save tasks from tmp file to task_repo

Returns:

  • (TrueClass)


18
19
20
21
22
23
# File 'lib/ruby/pomodoro/tasks/editor.rb', line 18

def edit
  save
  editor.open(file_path)
  create_tasks
  true
end

#loadTrueClass

Load tasks form file

Returns:

  • (TrueClass)


38
39
40
41
# File 'lib/ruby/pomodoro/tasks/editor.rb', line 38

def load
  create_tasks
  true
end

#saveTrueClass

save list to disc

Returns:

  • (TrueClass)


27
28
29
30
31
32
33
34
# 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