Class: Blackcal::TimeOfDay

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/blackcal/time_of_day.rb

Overview

Represents a time of day (hour and min)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hour, min = nil) ⇒ TimeOfDay

Initialize time of day

Parameters:

  • hour (Integer)
  • min (Integer, nil) (defaults to: nil)

    optional argument



17
18
19
20
# File 'lib/blackcal/time_of_day.rb', line 17

def initialize(hour, min = nil)
  @hour = hour
  @min = min || 0
end

Instance Attribute Details

#hourInteger (readonly)

Returns hour.

Returns:

  • (Integer)

    hour



9
10
11
# File 'lib/blackcal/time_of_day.rb', line 9

def hour
  @hour
end

#minInteger (readonly)

Returns minutes defaults to 0.

Returns:

  • (Integer)

    minutes defaults to 0



12
13
14
# File 'lib/blackcal/time_of_day.rb', line 12

def min
  @min
end

Instance Method Details

#<=>(other) ⇒ Integer

Compares two time of days

Parameters:

  • other (TimeOfDay, Integer)

    if a number is passed it will be used as the hour

Returns:

  • (Integer)

    1 if greater than, 0 if equal, -1 if less than



25
26
27
28
29
30
31
32
33
34
# File 'lib/blackcal/time_of_day.rb', line 25

def <=>(other)
  other_seconds = if other.is_a?(self.class)
                    (other.hour * 60 * 60) + (other.min * 60)
                  else
                    other * 60 * 60
                  end
  seconds = (hour * 60 * 60) + (min * 60)

  seconds <=> other_seconds
end