Class: OpeningHours
- Inherits:
-
Object
- Object
- OpeningHours
- Defined in:
- lib/opening_hours.rb,
lib/opening_hours/version.rb
Defined Under Namespace
Modules: TimeUtils Classes: OpenHours
Constant Summary collapse
- WEEK_DAYS =
Time::RFC2822_DAY_NAME.map { |m| m.downcase.to_sym }
- VERSION =
"0.0.7"
Instance Attribute Summary collapse
-
#breaks ⇒ Object
readonly
makes it possible to access each day like: b.week.open, or iterate through all weekdays.
-
#week ⇒ Object
readonly
makes it possible to access each day like: b.week.open, or iterate through all weekdays.
Instance Method Summary collapse
- #break(day, start_time, end_time) ⇒ Object
- #calculate_deadline(job_duration, start_date_time) ⇒ Object
- #closed(*days) ⇒ Object
-
#initialize(start_time, end_time, time_zone = 'Europe/London') ⇒ OpeningHours
constructor
A new instance of OpeningHours.
- #now_open? ⇒ Boolean
- #update(day, start_time, end_time) ⇒ Object
Constructor Details
#initialize(start_time, end_time, time_zone = 'Europe/London') ⇒ OpeningHours
Returns a new instance of OpeningHours.
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/opening_hours.rb', line 61 def initialize(start_time, end_time, time_zone = 'Europe/London') Time.zone = time_zone open_hours = OpenHours.parse(start_time, end_time) @week = {} WEEK_DAYS.each do |day| @week[day] = open_hours end @specific_days = {} @breaks = {} end |
Instance Attribute Details
#breaks ⇒ Object (readonly)
makes it possible to access each day like: b.week.open, or iterate through all weekdays
57 58 59 |
# File 'lib/opening_hours.rb', line 57 def breaks @breaks end |
#week ⇒ Object (readonly)
makes it possible to access each day like: b.week.open, or iterate through all weekdays
57 58 59 |
# File 'lib/opening_hours.rb', line 57 def week @week end |
Instance Method Details
#break(day, start_time, end_time) ⇒ Object
85 86 87 |
# File 'lib/opening_hours.rb', line 85 def break(day, start_time, end_time) set_break_hours day, OpenHours.parse(start_time, end_time) end |
#calculate_deadline(job_duration, start_date_time) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/opening_hours.rb', line 93 def calculate_deadline(job_duration, start_date_time) start_date_time = Time.zone.parse(start_date_time) today = Date.civil(start_date_time.year, start_date_time.month, start_date_time.day) open_hours = get_open_hours(today).offset(TimeUtils::seconds_from_midnight(start_date_time)) break_hours_duration = get_break_duration(today) break_hours = get_break_hours(today) # here is possible to use strict greater operator if you want to stop on edge of previous business day. # see "BusinessHours schedule without exceptions should flip the edge" spec while job_duration >= open_hours.duration job_duration -= open_hours.duration today = today.next open_hours = get_open_hours(today) break_hours = get_break_hours(today) end if (open_hours.open + job_duration).between?(break_hours.open, break_hours.close) job_duration += break_hours_duration end Time.zone.local(today.year, today.month, today.day, *TimeUtils::time_from_midnight(open_hours.open + job_duration)).to_formatted_s(:rfc822) end |
#closed(*days) ⇒ Object
79 80 81 82 83 |
# File 'lib/opening_hours.rb', line 79 def closed(*days) days.each do |day| set_open_hours day, OpenHours::CLOSED end end |
#now_open? ⇒ Boolean
89 90 91 |
# File 'lib/opening_hours.rb', line 89 def now_open? calculate_deadline(0, Time.zone.now.to_s) == Time.zone.now.to_formatted_s(:rfc822) end |