Class: Timing::HourMinutesSeconds
- Inherits:
-
Object
- Object
- Timing::HourMinutesSeconds
- 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
-
#hour ⇒ Object
readonly
Returns the value of attribute hour.
-
#minutes ⇒ Object
readonly
Returns the value of attribute minutes.
-
#seconds ⇒ Object
readonly
Returns the value of attribute seconds.
Class Method Summary collapse
Instance Method Summary collapse
- #<(other) ⇒ Object
- #<=(other) ⇒ Object
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object (also: #eql?)
- #>(other) ⇒ Object
- #>=(other) ⇒ Object
- #between?(from, to) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(hour, minutes = 0, seconds = 0) ⇒ HourMinutesSeconds
constructor
A new instance of HourMinutesSeconds.
- #iso8601 ⇒ Object (also: #to_s, #inspect)
Constructor Details
#initialize(hour, minutes = 0, seconds = 0) ⇒ HourMinutesSeconds
Returns a new instance of HourMinutesSeconds.
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
#hour ⇒ Object (readonly)
Returns the value of attribute hour.
6 7 8 |
# File 'lib/timing/hour_minutes_seconds.rb', line 6 def hour @hour end |
#minutes ⇒ Object (readonly)
Returns the value of attribute minutes.
6 7 8 |
# File 'lib/timing/hour_minutes_seconds.rb', line 6 def minutes @minutes end |
#seconds ⇒ Object (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
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
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
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
67 68 69 |
# File 'lib/timing/hour_minutes_seconds.rb', line 67 def between?(from, to) self >= from && self <= to end |
#hash ⇒ Object
29 30 31 |
# File 'lib/timing/hour_minutes_seconds.rb', line 29 def hash [hour, minutes, seconds].hash end |
#iso8601 ⇒ Object 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 |