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:

  • 0.2


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

def initialize(data)
  @data = data

  unless data.is_a?(Array) && data.all? { |item| item.respond_to?(:header) && item.respond_to?(:tasks) }
    raise ArgumentError, "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:

  • 0.2


9
10
11
# File 'lib/pomodoro/formats/scheduled/task_list.rb', line 9

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:

  • 0.2


50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/pomodoro/formats/scheduled/task_list.rb', line 50

def <<(task_group)
  unless task_group.is_a?(TaskGroup)
    raise ArgumentError, "TaskGroup expected, got #{task_group.class}."
  end

  if self[task_group.header]
    raise ArgumentError, "Task group with header #{task_group.header} is already on the list."
  end

  @data << task_group
  self.sort!
end

#[](header) ⇒ TaskGroup?

Find a task group that matches given header.

Examples:

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

Returns:

Since:

  • 0.2


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

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:

  • 0.2


82
83
84
# File 'lib/pomodoro/formats/scheduled/task_list.rb', line 82

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

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

Iterate over the task groups.

Yield Parameters:

Since:

  • 0.2


90
91
92
# File 'lib/pomodoro/formats/scheduled/task_list.rb', line 90

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

#non_scheduled_task_groupsObject


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

def non_scheduled_task_groups
  @data.group_by { |tg| tg.scheduled_date.nil? }[true] || Array.new
end

#save(path) ⇒ Object


101
102
103
104
105
106
# File 'lib/pomodoro/formats/scheduled/task_list.rb', line 101

def save(path)
  data = self.to_s
  File.open(path, 'w:utf-8') do |file|
    file.puts(data)
  end
end

#scheduled_task_groupsObject


63
64
65
# File 'lib/pomodoro/formats/scheduled/task_list.rb', line 63

def scheduled_task_groups
  @data.group_by { |tg| tg.scheduled_date.nil? }[false] || Array.new
end

#sort!Object


71
72
73
74
75
76
# File 'lib/pomodoro/formats/scheduled/task_list.rb', line 71

def sort!
  @data = self.scheduled_task_groups.sort_by(&:scheduled_date) +
          self.non_scheduled_task_groups

  self
end

#to_sObject

Return a scheduled task list formatted string.

Since:

  • 0.2


97
98
99
# File 'lib/pomodoro/formats/scheduled/task_list.rb', line 97

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