Class: Pomodoro::Formats::Today::TimeFrame

Inherits:
Object
  • Object
show all
Defined in:
lib/pomodoro/formats/today/time_frame.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, start_time: nil, end_time: nil, items: Array.new) ⇒ TimeFrame

Returns a new instance of TimeFrame.

Examples:

require 'pomodoro/formats/today'

time_frame = Pomodoro::Formats::Today::TimeFrame.new(
  name: 'Morning routine', start_time: Hour.parse('7:50'), items: [
    Pomodoro::Formats::Today::Task.new(status: :done, body: 'Headspace.')
  ]
)

Parameters:

  • name (String)

    name of the task group.

  • start_time (Hour) (defaults to: nil)

    of when the time frame starts.

  • end_time (Hour) (defaults to: nil)

    of when the time frame ends.

  • tasks (Array<Task>)

    tasks of the group.

Raises:

  • ArgumentError if name is not present, neither start_time or end_time is present or one of the times is not an Hour instance or if tasks is not an array of task-like objects.

Since:

  • 0.2


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/pomodoro/formats/today/time_frame.rb', line 42

def initialize(name:, start_time: nil, end_time: nil, items: Array.new)
  @name, @start_time, @end_time, @items = name, start_time, end_time, items

  # This is not true, because it can be determined by the next/previous time frame.
  # https://github.com/botanicus/now-task-manager/issues/20
  # if @start_time.nil? && @end_time.nil?
  #   raise ArgumentError.new("At least one of start_time and end_time has to be provided.")
  # end

  unless [@start_time, @end_time].compact.all? { |time| time.is_a?(Hour) }
    raise ArgumentError, "Start time and end time has to be an Hour instance."
  end

  task_methods = [:status, :body, :start_time, :end_time, :metadata]
  unless items.is_a?(Array) && items.all? { |item| item.is_a?(Task) }
    raise ArgumentError, "Items is supposed to be an array of Task instances."
  end
end

Instance Attribute Details

#end_timeObject (readonly)

Since:

  • 0.2


23
24
25
# File 'lib/pomodoro/formats/today/time_frame.rb', line 23

def end_time
  @end_time
end

#itemsObject (readonly)

Since:

  • 0.2


23
24
25
# File 'lib/pomodoro/formats/today/time_frame.rb', line 23

def items
  @items
end

#nameObject (readonly)

Since:

  • 0.2


23
24
25
# File 'lib/pomodoro/formats/today/time_frame.rb', line 23

def name
  @name
end

#start_timeObject (readonly)

Since:

  • 0.2


23
24
25
# File 'lib/pomodoro/formats/today/time_frame.rb', line 23

def start_time
  @start_time
end

Instance Method Details

#active?(current_time = Hour.now, prev_time_frame_end_time = nil, next_time_frame_start_time = nil) ⇒ Boolean

Return true or false based on whether the time frame is active in the provided current_time.

Examples:

# TODO

Parameters:

  • current_time (Hour) (defaults to: Hour.now)
  • prev_time_frame_end_time (Hour) (defaults to: nil)
  • next_time_frame_end_time (Hour)

Returns:

  • (Boolean)

Raises:

Since:

  • 0.2


107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/pomodoro/formats/today/time_frame.rb', line 107

def active?(current_time = Hour.now, prev_time_frame_end_time = nil, next_time_frame_start_time = nil)
  unless current_time.is_a?(Hour)
    raise ArgumentError, "Current time has to be an Hour instance, was #{current_time.class}."
  end

  start_time = @start_time || (prev_time_frame_end_time || Hour.parse('0:00'))
  end_time = @end_time || (next_time_frame_start_time || Hour.parse('23:59'))

  validate_time_info_consistency(start_time, end_time)

  start_time <= current_time && end_time > current_time
end

#active_taskObject


173
174
175
# File 'lib/pomodoro/formats/today/time_frame.rb', line 173

def active_task
  self.tasks.find(&:in_progress?)
end

#actual_durationObject


90
91
92
93
# File 'lib/pomodoro/formats/today/time_frame.rb', line 90

def actual_duration
  last_finished_task = self.tasks.reverse.find(&:end_time)
  last_finished_task.end_time - self.tasks.first.start_time
end

#create_task(body, *tags) ⇒ Object


155
156
157
158
159
# File 'lib/pomodoro/formats/today/time_frame.rb', line 155

def create_task(body, *tags)
  task = Task.new(status: :not_done, body: body, tags: tags)
  @items << task
  task
end

#duration(prev_time_frame_end_time = nil, next_time_frame_start_time = nil) ⇒ Hour

Return overall duration of the time frame.

Examples:

# TODO

Parameters:

  • prev_time_frame_end_time (Hour) (defaults to: nil)
  • next_time_frame_end_time (Hour)

Returns:

  • (Hour)

Raises:

Since:

  • 0.2


75
76
77
78
79
80
81
82
# File 'lib/pomodoro/formats/today/time_frame.rb', line 75

def duration(prev_time_frame_end_time = nil, next_time_frame_start_time = nil)
  start_time = @start_time || prev_time_frame_end_time
  end_time = @end_time || next_time_frame_start_time

  validate_time_info_consistency(start_time, end_time)

  end_time - start_time
end

#duration_Object

TODO: Rename neco jako cistyho casu.


84
85
86
87
88
# File 'lib/pomodoro/formats/today/time_frame.rb', line 84

def duration_ # TODO: Rename neco jako cistyho casu.
  self.tasks.reduce(0) do |sum, task|
    (task.ended? && task.actual_duration) ? task.actual_duration + sum : sum
  end
end

#ended?(current_time = Hour.now, next_time_frame_start_time = nil) ⇒ Boolean

Returns:

  • (Boolean)

120
121
122
123
124
125
126
127
128
# File 'lib/pomodoro/formats/today/time_frame.rb', line 120

def ended?(current_time = Hour.now, next_time_frame_start_time = nil)
  unless current_time.is_a?(Hour)
    raise ArgumentError, "Current time has to be an Hour instance, was #{current_time.class}."
  end

  end_time = @end_time || (next_time_frame_start_time || Hour.parse('23:59'))

  end_time > current_time
end

#first_unstarted_taskObject


169
170
171
# File 'lib/pomodoro/formats/today/time_frame.rb', line 169

def first_unstarted_task
  self.tasks.find(&:unstarted?)
end

#headerObject


177
178
179
180
181
182
183
184
185
# File 'lib/pomodoro/formats/today/time_frame.rb', line 177

def header
  if @start_time && @end_time
    [@name, "(#{@start_time}#{@end_time})"].compact.join(' ')
  elsif @start_time && !@end_time
    [@name, "(from #{@start_time})"].compact.join(' ')
  elsif !@start_time && @end_time
    [@name, "(until #{@end_time})"].compact.join(' ')
  end
end

#method_nameObject

Name of method that will be available on a task list to access a time frame.

Examples:

Pomodoro::Formats::Today::TimeFrame.new(
  name: "Morning routine",
  start_time: Hour.parse('7:50')
).method_name

# => :morning_routine

Since:

  • 0.2


151
152
153
# File 'lib/pomodoro/formats/today/time_frame.rb', line 151

def method_name
  @name.downcase.tr(' ', '_').to_sym
end

#remaining_durationObject

def clear @tasks.clear end


165
166
167
# File 'lib/pomodoro/formats/today/time_frame.rb', line 165

def remaining_duration
  @end_time && (@end_time - Hour.now)
end

#tasksObject


61
62
63
# File 'lib/pomodoro/formats/today/time_frame.rb', line 61

def tasks
  self.items.select { |item| item.is_a?(Task) }
end

#to_sObject

Return a today task list formatted string.

Since:

  • 0.2


133
134
135
136
137
138
139
# File 'lib/pomodoro/formats/today/time_frame.rb', line 133

def to_s
  if self.items.empty?
    "#{self.header}\n"
  else
    "#{self.header}\n#{self.items.map(&:to_s).join}"
  end
end