Class: Domodoro::Timepoint

Inherits:
Object
  • Object
show all
Defined in:
lib/domodoro/timepoint.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Timepoint

Returns a new instance of Timepoint.



5
6
7
8
9
10
11
12
# File 'lib/domodoro/timepoint.rb', line 5

def initialize(*args)
  case args.first
  when String
    @hour, @min = args.first.split(':').map(&:to_i)
  when Fixnum
    @hour, @min = args.first, args.last
  end
end

Instance Attribute Details

#hourObject (readonly)

Returns the value of attribute hour.



3
4
5
# File 'lib/domodoro/timepoint.rb', line 3

def hour
  @hour
end

#minObject (readonly)

Returns the value of attribute min.



3
4
5
# File 'lib/domodoro/timepoint.rb', line 3

def min
  @min
end

Instance Method Details

#+(minutes) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/domodoro/timepoint.rb', line 14

def +(minutes)
  hours_to_advance = ((@min + minutes) / 60).to_i

  h = @hour + hours_to_advance
  m = (@min + minutes) % 60

  Timepoint.new("#{h}:#{m}")
end

#==(timepoint) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/domodoro/timepoint.rb', line 43

def ==(timepoint)
  case timepoint
  when String
    Timepoint.new(timepoint) == self
  when Timepoint
    @hour == timepoint.hour && @min == timepoint.min
  end
end

#after?(timepoint) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
# File 'lib/domodoro/timepoint.rb', line 23

def after?(timepoint)
  if @hour > timepoint.hour
    true
  elsif @hour == timepoint.hour
    @min >= timepoint.min
  else
    false
  end
end

#before?(timepoint) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
# File 'lib/domodoro/timepoint.rb', line 33

def before?(timepoint)
  if @hour < timepoint.hour
    true
  elsif @hour == timepoint.hour
    @min < timepoint.min
  else
    false
  end
end

#left_until(timestamp) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/domodoro/timepoint.rb', line 58

def left_until(timestamp)
  secs = 59 - Time.now.sec
  hours = timestamp.hour - @hour

  if timestamp.hour == @hour
    remaining_minutes = (timestamp.min - @min - 1).to_s.rjust(2, '0')
    return "00:#{remaining_minutes}:#{secs.to_s.rjust(2, '0')}"
  end

  if timestamp.min < @min
    hours -= 1
  end

  mins = ((timestamp.min - @min) % 60) - 1

  h = hours.to_s.rjust(2, '0')
  m = mins.to_s.rjust(2, '0')
  s = secs.to_s.rjust(2, '0')

  "#{h}:#{m}:#{s}"
end

#to_sObject



52
53
54
55
56
# File 'lib/domodoro/timepoint.rb', line 52

def to_s
  [@hour, @min].map(&:to_s).map do |number|
    number.rjust(2, '0')
  end.join(':')
end