Class: Reservation::Schedule::HourMinute

Inherits:
Object
  • Object
show all
Defined in:
lib/reservation/hour_minute.rb

Overview

a utility class to match the hour and minute elements of a Time instance, without considering any other values

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hour, minute) ⇒ HourMinute

Returns a new instance of HourMinute.



9
10
11
# File 'lib/reservation/hour_minute.rb', line 9

def initialize hour, minute
  @hour, @minute = hour, minute
end

Instance Attribute Details

#hourObject

Returns the value of attribute hour.



7
8
9
# File 'lib/reservation/hour_minute.rb', line 7

def hour
  @hour
end

#minuteObject

Returns the value of attribute minute.



7
8
9
# File 'lib/reservation/hour_minute.rb', line 7

def minute
  @minute
end

Class Method Details

.parse(hhmm) ⇒ Object

hhmm is a string containg an hour-and-minute value

#parse will remove all nondigit characters, pad the result to 4 digits, and interpret the first two as an hour value, the last two as a minute value

padding takes place as follows :

* one digit becomes 0d00 (assumes "7" means "0700")
* two digits become dd00 (assumes "11" means "1100")
* three digits become 0ddd (assumes "830" means "0830")


28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/reservation/hour_minute.rb', line 28

def self.parse hhmm
  orig = hhmm
  hhmm = hhmm.gsub(/[^\d]/, "")
  hhmm = "0#{hhmm}00" if hhmm.length == 1
  hhmm = "#{hhmm}00" if hhmm.length == 2
  hhmm = "0#{hhmm}" if hhmm.length == 3
  raise "Can't parse #{orig.inspect}" unless hhmm.match(/^\d\d\d\d$/)

  hh = hhmm[0,2].to_i
  mm = hhmm[2,4].to_i

  new hh, mm
end

Instance Method Details

#change(date) ⇒ Object



42
43
44
# File 'lib/reservation/hour_minute.rb', line 42

def change date
  date.to_time.change :hour => hour, :min => minute
end

#matches_time?(time) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/reservation/hour_minute.rb', line 13

def matches_time? time
  time.hour == self.hour && time.min == self.minute
end

#to_sObject



46
47
48
# File 'lib/reservation/hour_minute.rb', line 46

def to_s
  "#{hour.to_s.rjust 2, "0"}#{minute.to_s.rjust 2, "0"}"
end