Class: Pomodoro::Formats::Scheduled::TaskList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/pomodoro/formats/scheduled/task_list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ TaskList

Returns a new instance of TaskList.

Examples:

require 'pomodoro/formats/scheduled'

tasks = ['Buy milk. #errands', '[9:20] Call with Mike.']
group = Pomodoro::Formats::Scheduled::TaskGroup.new(header: 'Tomorrow', tasks: tasks)
list  = Pomodoro::Formats::Scheduled::TaskList.new([group])

Parameters:

  • data (Array<TaskGroup>)

    List of task groups. Or more precisely objects responding to ‘#header` and `#tasks`.

Raises:

  • (ArgumentError)

    if data is not an array or if its content doesn’t respond to ‘#header` and `#tasks`.

Since:

  • 1.0



21
22
23
24
25
26
27
# File 'lib/pomodoro/formats/scheduled/task_list.rb', line 21

def initialize(data)
  @data = data

  unless data.is_a?(Array) && data.all? { |item| item.respond_to?(:header) && item.respond_to?(:tasks) }
    raise ArgumentError.new("Data is supposed to be an array of TaskGroup instances.")
  end
end

Instance Attribute Details

#dataObject (readonly)

List of task groups. Or more precisely objects responding to ‘#header` and `#tasks`.

Since:

  • 1.0



7
8
9
# File 'lib/pomodoro/formats/scheduled/task_list.rb', line 7

def data
  @data
end

Instance Method Details

#<<(task_group) ⇒ Object

Add a task group onto the task list.

Parameters:

Raises:

  • (ArgumentError)

    if the task group is already in the list.

Since:

  • 1.0



48
49
50
51
52
53
54
# File 'lib/pomodoro/formats/scheduled/task_list.rb', line 48

def <<(task_group)
  if self[task_group.header]
    raise ArgumentError.new("Task group with header #{task_group.header} is already on the list.")
  end

  @data << task_group
end

#[](header) ⇒ TaskGroup?

Find a task group that matches given header.

Examples:

# Using the code from the initialiser.
list['Tomorrow']

Returns:

Since:

  • 1.0



37
38
39
40
41
# File 'lib/pomodoro/formats/scheduled/task_list.rb', line 37

def [](header)
  @data.find do |task_group|
    task_group.header == header
  end
end

#delete(task_group) ⇒ Object

Remove a task group from the task list.

Parameters:

Since:

  • 1.0



60
61
62
# File 'lib/pomodoro/formats/scheduled/task_list.rb', line 60

def delete(task_group)
  @data.delete(task_group)
end

#each {|task_group| ... } ⇒ Object

Iterate over the task groups.

Yield Parameters:

Since:

  • 1.0



68
69
70
# File 'lib/pomodoro/formats/scheduled/task_list.rb', line 68

def each(&block)
  @data.each(&block)
end

#save(destination_path) ⇒ Object

Save scheduled task list formatted string into a file.

Parameters:

  • destination_path (String)

Since:

  • 1.0



83
84
85
86
87
88
# File 'lib/pomodoro/formats/scheduled/task_list.rb', line 83

def save(destination_path)
  data = self.to_s
  File.open(destination_path, 'w') do |file|
    file.puts(data)
  end
end

#to_sObject

Return a scheduled task list formatted string.

Since:

  • 1.0



75
76
77
# File 'lib/pomodoro/formats/scheduled/task_list.rb', line 75

def to_s
  @data.map(&:to_s).join("\n")
end