Class: OpeningHours::OpenHours

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

Constant Summary collapse

CLOSED =
new(0, 0)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(open, close) ⇒ OpenHours

Returns a new instance of OpenHours.



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

def initialize(open, close)
  @open, @close = open, close
end

Instance Attribute Details

#closeObject (readonly)

Returns the value of attribute close.



10
11
12
# File 'lib/opening_hours.rb', line 10

def close
  @close
end

#openObject (readonly)

Returns the value of attribute open.



10
11
12
# File 'lib/opening_hours.rb', line 10

def open
  @open
end

Class Method Details

.parse(open, close) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/opening_hours.rb', line 22

def self.parse(open, close)
  open  = Time.zone.parse(open)
  close = Time.zone.parse(close)

  open  = TimeUtils::seconds_from_midnight(open)
  close = TimeUtils::seconds_from_midnight(close)

  new(open, close)
end

Instance Method Details

#durationObject



16
17
18
# File 'lib/opening_hours.rb', line 16

def duration
  @duration ||= @open < @close ? @close - @open : 0
end

#offset(seconds) ⇒ Object



32
33
34
# File 'lib/opening_hours.rb', line 32

def offset(seconds)
  self.class.new([@open, seconds].max, @close)
end