Class: Gadgeto::TimeOfDay
- Inherits:
-
Object
- Object
- Gadgeto::TimeOfDay
- Defined in:
- lib/gadgeto/time_of_day.rb
Overview
A TimeOfDay
represents the time of day.
Limitation: 24 hours only.
Defined Under Namespace
Classes: InvalidTimeOfDayFormat, TimeOfDayOutOfRange
Constant Summary collapse
- ONE_HOUR_IN_MINUTES =
:nodoc:
60
- HOURS_ONE_DAY =
:nodoc:
24
Instance Attribute Summary collapse
-
#hour ⇒ Object
readonly
Returns the hour component.
-
#minute ⇒ Object
readonly
Returns the hour component.
Class Method Summary collapse
-
.valid?(time_of_day) ⇒ Boolean
Returns
true
iftime_of_day
represents time of day.
Instance Method Summary collapse
-
#<(other) ⇒ Object
Returns
true
ifself
precedesother
. -
#==(other) ⇒ Object
Returns true if
self
is equal toother
. -
#>(other) ⇒ Object
Returns
true
ifself
followsother
. -
#add_minutes(minutes) ⇒ Object
Adds the given
minutes
toself
. -
#initialize(time_of_day) ⇒ TimeOfDay
constructor
Creates a new TimeOfDay object.
-
#minutes_till(other, options = {}) ⇒ Object
Returns difference in minutes between
self
andother
. -
#to_i ⇒ Object
Returns time of day in minutes.
-
#to_s ⇒ Object
(also: #inspect)
Returns string representing time of day.
Constructor Details
#initialize(time_of_day) ⇒ TimeOfDay
23 24 25 26 27 28 29 |
# File 'lib/gadgeto/time_of_day.rb', line 23 def initialize(time_of_day) match = /^([0-2]?\d):([0-5]\d)$/.match(time_of_day) raise InvalidTimeOfDayFormat if match.nil? @hour = match[1].to_i @minute = match[2].to_i raise InvalidTimeOfDayFormat if (@hour > HOURS_ONE_DAY) || (@hour == HOURS_ONE_DAY && @minute > 0) end |
Instance Attribute Details
#hour ⇒ Object (readonly)
35 36 37 |
# File 'lib/gadgeto/time_of_day.rb', line 35 def hour @hour end |
Class Method Details
.valid?(time_of_day) ⇒ Boolean
Returns true
if time_of_day
represents time of day.
TimeOfDay.valid?("08:00") #=> true
130 131 132 133 134 135 136 137 |
# File 'lib/gadgeto/time_of_day.rb', line 130 def self.valid?(time_of_day) begin val = TimeOfDay.new(time_of_day) true rescue InvalidTimeOfDayFormat, TimeOfDayOutOfRange false end end |
Instance Method Details
#<(other) ⇒ Object
Returns true
if self
precedes other
.
TimeOfDay.new("08:00") < TimeOfDay.new("09:00:) #=> true
82 83 84 85 86 87 |
# File 'lib/gadgeto/time_of_day.rb', line 82 def <(other) return true if self.hour < other.hour return false if self.hour > other.hour self.minute < other.minute end |
#==(other) ⇒ Object
102 103 104 |
# File 'lib/gadgeto/time_of_day.rb', line 102 def ==(other) self.hour == other.hour && self.minute == other.minute end |
#>(other) ⇒ Object
Returns true
if self
follows other
.
TimeOfDay.new("09:00") < TimeOfDay.new("08:00:) #=> true
92 93 94 95 96 97 |
# File 'lib/gadgeto/time_of_day.rb', line 92 def >(other) return true if self.hour > other.hour return false if self.hour < other.hour self.minute > other.minute end |
#add_minutes(minutes) ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/gadgeto/time_of_day.rb', line 54 def add_minutes(minutes) new_minutes = @minute + minutes hours_to_add = (@minute + minutes) / 60 new_hour = @hour + hours_to_add @hour = new_hour % HOURS_ONE_DAY @minute = new_minutes % ONE_HOUR_IN_MINUTES self end |
#minutes_till(other, options = {}) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/gadgeto/time_of_day.rb', line 112 def minutes_till(other, = {}) [:overflow] = true if [:overflow].nil? if [:overflow] if self > other (self.minutes_till(TimeOfDay.new("24:00"))) + other.to_i else other.to_i - self.to_i end else raise TimeOfDayOutOfRange if self > other other.to_i - self.to_i end end |
#to_i ⇒ Object
75 76 77 |
# File 'lib/gadgeto/time_of_day.rb', line 75 def to_i hour * 60 + minute end |