Class: OpeningHoursConverter::OpeningHoursTime

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/opening_hours_converter/opening_hours_time.rb

Constant Summary

Constants included from Constants

Constants::DAYS, Constants::DAYS_MAX, Constants::EASTER_WEEKDAY, Constants::IRL_DAYS, Constants::IRL_MONTHS, Constants::MINUTES_MAX, Constants::MONTH_END_DAY, Constants::OSM_DAYS, Constants::OSM_DAYS_ABBREVIATIONS, Constants::OSM_MONTHS, Constants::PH_WEEKDAY, Constants::YEAR_DAYS_MAX

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(minute_start = nil, minute_end = nil, open_ended = false) ⇒ OpeningHoursTime

Returns a new instance of OpeningHoursTime.



9
10
11
12
13
# File 'lib/opening_hours_converter/opening_hours_time.rb', line 9

def initialize(minute_start = nil, minute_end = nil, open_ended = false)
  @start = minute_start
  @end = minute_end unless minute_start == minute_end
  @open_ended = open_ended
end

Instance Attribute Details

#endObject (readonly)

Returns the value of attribute end.



7
8
9
# File 'lib/opening_hours_converter/opening_hours_time.rb', line 7

def end
  @end
end

#open_endedObject (readonly)

Returns the value of attribute open_ended.



7
8
9
# File 'lib/opening_hours_converter/opening_hours_time.rb', line 7

def open_ended
  @open_ended
end

#priorityObject (readonly)

Returns the value of attribute priority.



7
8
9
# File 'lib/opening_hours_converter/opening_hours_time.rb', line 7

def priority
  @priority
end

#startObject (readonly)

Returns the value of attribute start.



7
8
9
# File 'lib/opening_hours_converter/opening_hours_time.rb', line 7

def start
  @start
end

Instance Method Details

#equals(t) ⇒ Object



28
29
30
# File 'lib/opening_hours_converter/opening_hours_time.rb', line 28

def equals(t)
  @start == t.start && @end == t.end
end

#getObject



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/opening_hours_converter/opening_hours_time.rb', line 15

def get
  return 'off' if @start.nil? && @end.nil?
  # An open end names no closing time, so a time that already runs to
  # midnight writes itself as the bare "18:00+"; one that closes earlier
  # keeps the hours it does name ("10:00-12:00+").
  if @open_ended
    return "#{time_string(@start)}+" if @end.nil? || @end == MINUTES_MAX

    return "#{time_string(@start)}-#{time_string(@end)}+"
  end
  "#{time_string(@start)}#{@end.nil? ? '' : "-#{time_string(@end)}"}"
end

#time_string(minutes) ⇒ Object



32
33
34
35
36
37
# File 'lib/opening_hours_converter/opening_hours_time.rb', line 32

def time_string(minutes)
  fminutes = minutes.to_f
  h = (fminutes / 60).floor.to_i
  m = (fminutes % 60).to_i
  "#{h < 10 ? '0' : ''}#{h}:#{m < 10 ? '0' : ''}#{m}"
end