Class: Pomodoro::Formats::Scheduled::TaskList
- Inherits:
-
Object
- Object
- Pomodoro::Formats::Scheduled::TaskList
- Includes:
- Enumerable
- Defined in:
- lib/pomodoro/formats/scheduled/task_list.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
List of task groups.
Instance Method Summary collapse
-
#<<(task_group) ⇒ Object
Add a task group onto the task list.
-
#[](header) ⇒ TaskGroup?
Find a task group that matches given header.
-
#delete(task_group) ⇒ Object
Remove a task group from the task list.
-
#each {|task_group| ... } ⇒ Object
Iterate over the task groups.
-
#initialize(data) ⇒ TaskList
constructor
A new instance of TaskList.
-
#save(destination_path) ⇒ Object
Save scheduled task list formatted string into a file.
-
#to_s ⇒ Object
Return a scheduled task list formatted string.
Constructor Details
#initialize(data) ⇒ TaskList
Returns a new instance of TaskList.
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
#data ⇒ Object (readonly)
List of task groups. Or more precisely objects responding to ‘#header` and `#tasks`.
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.
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.
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.
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.
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.
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_s ⇒ Object
Return a scheduled task list formatted string.
75 76 77 |
# File 'lib/pomodoro/formats/scheduled/task_list.rb', line 75 def to_s @data.map(&:to_s).join("\n") end |