Class: Pomodoro::Formats::Scheduled::TaskGroup

Inherits:
Object
  • Object
show all
Defined in:
lib/pomodoro/formats/scheduled/task_group.rb

Constant Summary collapse

DATE_FORMATS =

TODO: Next Monday NOTE: For parsing we don't use %-d etc, only %d.

{
  '%d/%m'    => :next_month,    # 1/1
  '%d/%m/%Y' => nil,            # 1/1/2018
  '%A %d/%m' => :next_week,     # Monday 1/1  Note: Higher specifity has to come first.
  '%A'       => :next_week      # Monday
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(header:, tasks: Array.new) ⇒ TaskGroup

Returns a new instance of TaskGroup.

Examples:

require 'pomodoro/formats/scheduled'

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

Parameters:

  • header (String)

    header of the task group.

  • tasks (Array<String>) (defaults to: Array.new)

    tasks of the group.

Since:

  • 0.2


19
20
21
22
23
24
# File 'lib/pomodoro/formats/scheduled/task_group.rb', line 19

def initialize(header:, tasks: Array.new)
  @header, @tasks = header, tasks
  if tasks.any? { |task| !task.is_a?(Task) }
    raise ArgumentError, "Task objects expected."
  end
end

Instance Attribute Details

#headerObject (readonly)

Since:

  • 0.2


8
9
10
# File 'lib/pomodoro/formats/scheduled/task_group.rb', line 8

def header
  @header
end

#tasksObject (readonly)

Since:

  • 0.2


8
9
10
# File 'lib/pomodoro/formats/scheduled/task_group.rb', line 8

def tasks
  @tasks
end

Instance Method Details

#<<(task) ⇒ Object

Add a task to the task group.

Raises:

  • (ArgumentError)

Since:

  • 0.2


29
30
31
32
33
# File 'lib/pomodoro/formats/scheduled/task_group.rb', line 29

def <<(task)
  raise ArgumentError, "Task expected, got #{task.class}." unless task.is_a?(Task)

  @tasks << task unless @tasks.map(&:to_s).include?(task.to_s)
end

#delete(task) ⇒ Object

Remove a task from the task group.

Raises:

  • (ArgumentError)

Since:

  • 0.2


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

def delete(task)
  raise ArgumentError, "Task expected, got #{task.class}." unless task.is_a?(Task)

  @tasks.delete_if { |t2| t2.to_s == task.to_s }
end

#save(path) ⇒ Object


51
52
53
54
55
56
# File 'lib/pomodoro/formats/scheduled/task_group.rb', line 51

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

#scheduled_dateObject

labels = ['Tomorrow', date.strftime('%A'), date.strftime('%-d/%m'), date.strftime('%-d/%m/%Y')]


68
69
70
71
72
# File 'lib/pomodoro/formats/scheduled/task_group.rb', line 68

def scheduled_date
  return Date.today if @header == 'Today' # Change tomorrow to Today if you're generating it in the morning.
  return Date.today + 1 if @header == 'Tomorrow'
  parse_date_in_the_future(@header)
end

#to_sObject

Return a scheduled task list formatted string.

Since:

  • 0.2


47
48
49
# File 'lib/pomodoro/formats/scheduled/task_group.rb', line 47

def to_s
  [@header, @tasks.map(&:to_s), nil].flatten.join("\n")
end

#tomorrow?Boolean

Returns:

  • (Boolean)

74
75
76
# File 'lib/pomodoro/formats/scheduled/task_group.rb', line 74

def tomorrow?
  self.scheduled_date == Date.today + 1
end