Class: Timing::HourMinutesSeconds

Inherits:
Object
  • Object
show all
Defined in:
lib/timing/hour_minutes_seconds.rb

Constant Summary collapse

HH_MM_SS_REGEX =
/^(?<hour>\d{1,2})(:(?<minutes>\d{1,2}))?(:(?<seconds>\d{1,2}))?$/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hour, minutes = 0, seconds = 0) ⇒ HourMinutesSeconds

Returns a new instance of HourMinutesSeconds.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
# File 'lib/timing/hour_minutes_seconds.rb', line 8

def initialize(hour, minutes=0, seconds=0)
  raise ArgumentError, "Invalid hour #{hour}" if !hour.is_a?(Integer) || hour < 0 || hour > 23
  raise ArgumentError, "Invalid minutes #{minutes}" if !minutes.is_a?(Integer) || minutes < 0 || minutes > 59
  raise ArgumentError, "Invalid minutes #{seconds}" if !seconds.is_a?(Integer) || seconds < 0 || seconds > 59

  @hour = hour
  @minutes = minutes
  @seconds = seconds
end

Instance Attribute Details

#hourObject (readonly)

Returns the value of attribute hour.



6
7
8
# File 'lib/timing/hour_minutes_seconds.rb', line 6

def hour
  @hour
end

#minutesObject (readonly)

Returns the value of attribute minutes.



6
7
8
# File 'lib/timing/hour_minutes_seconds.rb', line 6

def minutes
  @minutes
end

#secondsObject (readonly)

Returns the value of attribute seconds.



6
7
8
# File 'lib/timing/hour_minutes_seconds.rb', line 6

def seconds
  @seconds
end

Class Method Details

.parse(expression) ⇒ Object

Raises:

  • (ArgumentError)


71
72
73
74
75
# File 'lib/timing/hour_minutes_seconds.rb', line 71

def self.parse(expression)
  match = expression.to_s.match HH_MM_SS_REGEX
  raise ArgumentError, "Invalid expression #{expression}" unless match
  new(*match.names.map { |n| (match[n] || 0).to_i })
end

Instance Method Details

#<(other) ⇒ Object

Raises:

  • (ArgumentError)


41
42
43
44
45
46
47
# File 'lib/timing/hour_minutes_seconds.rb', line 41

def <(other)
  raise ArgumentError, "Invalid argument #{other}" unless other.is_a?(HourMinutesSeconds)

  (hour < other.hour) ||
  (hour == other.hour && minutes < other.minutes) ||
  (hour == other.hour && minutes == other.minutes && seconds < other.seconds)
end

#<=(other) ⇒ Object



53
54
55
# File 'lib/timing/hour_minutes_seconds.rb', line 53

def <=(other)
  self < other || self == other
end

#<=>(other) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/timing/hour_minutes_seconds.rb', line 57

def <=>(other)
  if self == other
    0
  elsif self > other
    1
  else
    -1
  end
end

#==(other) ⇒ Object Also known as: eql?



24
25
26
# File 'lib/timing/hour_minutes_seconds.rb', line 24

def ==(other)
  other.kind_of?(self.class) && hash == other.hash
end

#>(other) ⇒ Object

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
# File 'lib/timing/hour_minutes_seconds.rb', line 33

def >(other)
  raise ArgumentError, "Invalid argument #{other}" unless other.is_a?(HourMinutesSeconds)

  (hour > other.hour) ||
  (hour == other.hour && minutes > other.minutes) ||
  (hour == other.hour && minutes == other.minutes && seconds > other.seconds)
end

#>=(other) ⇒ Object



49
50
51
# File 'lib/timing/hour_minutes_seconds.rb', line 49

def >=(other)
  self > other || self == other
end

#between?(from, to) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/timing/hour_minutes_seconds.rb', line 67

def between?(from, to)
  self >= from && self <= to
end

#hashObject



29
30
31
# File 'lib/timing/hour_minutes_seconds.rb', line 29

def hash
  [hour, minutes, seconds].hash
end

#iso8601Object Also known as: to_s, inspect



18
19
20
# File 'lib/timing/hour_minutes_seconds.rb', line 18

def iso8601
  [hour, minutes, seconds].map { |d| d.to_s.rjust(2, '0') }.join(':')
end