Class: BusinessTime::BusinessHours
- Inherits:
-
Object
- Object
- BusinessTime::BusinessHours
- Includes:
- Comparable
- Defined in:
- lib/business_time/business_hours.rb
Instance Attribute Summary collapse
-
#hours ⇒ Object
readonly
Returns the value of attribute hours.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #after(time) ⇒ Object (also: #since)
- #ago ⇒ Object
- #before(time) ⇒ Object
- #from_now ⇒ Object
-
#initialize(hours) ⇒ BusinessHours
constructor
A new instance of BusinessHours.
Constructor Details
#initialize(hours) ⇒ BusinessHours
Returns a new instance of BusinessHours.
7 8 9 |
# File 'lib/business_time/business_hours.rb', line 7 def initialize(hours) @hours = hours end |
Instance Attribute Details
#hours ⇒ Object (readonly)
Returns the value of attribute hours.
5 6 7 |
# File 'lib/business_time/business_hours.rb', line 5 def hours @hours end |
Instance Method Details
#<=>(other) ⇒ Object
11 12 13 14 15 16 |
# File 'lib/business_time/business_hours.rb', line 11 def <=>(other) if other.class != self.class raise ArgumentError.new("#{self.class.to_s} can't be compared with #{other.class.to_s}") end self.hours <=> other.hours end |
#after(time) ⇒ Object Also known as: since
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/business_time/business_hours.rb', line 26 def after(time) after_time = Time.roll_forward(time) # Step through the hours, skipping over non-business hours @hours.times do after_time = after_time + 1.hour if after_time.hour == 0 && after_time.min == 0 && after_time.sec == 0 after_time = Time.roll_forward(after_time) elsif (after_time > Time.end_of_workday(after_time)) # Ignore hours before opening and after closing delta = after_time - Time.end_of_workday(after_time) after_time = Time.roll_forward(after_time) + delta end # Ignore weekends and holidays while !after_time.workday? after_time = after_time + 1.day end end after_time end |
#ago ⇒ Object
18 19 20 |
# File 'lib/business_time/business_hours.rb', line 18 def ago Time.zone ? before(Time.zone.now) : before(Time.now) end |
#before(time) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/business_time/business_hours.rb', line 49 def before(time) before_time = Time.roll_backward(time) # Step through the hours, skipping over non-business hours @hours.times do before_time = before_time - 1.hour if before_time.hour == 0 && before_time.min == 0 && before_time.sec == 0 before_time = Time.roll_backward(before_time - 1.second) elsif (before_time <= Time.beginning_of_workday(before_time)) # Ignore hours before opening and after closing delta = Time.beginning_of_workday(before_time) - before_time # Due to the 23:59:59 end-of-workday exception time_roll_backward = Time.roll_backward(before_time) time_roll_backward += 1.second if time_roll_backward.to_s =~ /23:59:59/ before_time = time_roll_backward - delta end # Ignore weekends and holidays while !before_time.workday? before_time = before_time - 1.day end end before_time end |