Class: BusinessTime::BusinessHours

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

Instance Method Summary collapse

Constructor Details

#initialize(hours) ⇒ BusinessHours

Returns a new instance of BusinessHours.


4
5
6
# File 'lib/business_time/business_hours.rb', line 4

def initialize(hours)
  @hours = hours
end

Instance Method Details

#after(time) ⇒ Object


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/business_time/business_hours.rb', line 16

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

    # Ignore hours before opening and after closing
    if (after_time > Time.end_of_workday(after_time))
      after_time = after_time + off_hours
    end

    # Ignore weekends and holidays
    while !Time.workday?(after_time)
      after_time = after_time + 1.day
    end
  end
  after_time
end

#agoObject


8
9
10
# File 'lib/business_time/business_hours.rb', line 8

def ago
  Time.zone ? before(Time.zone.now) : before(Time.now)
end

#before(time) ⇒ Object


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/business_time/business_hours.rb', line 35

def before(time)
  before_time = Time.roll_forward(time)
  # Step through the hours, skipping over non-business hours
  @hours.times do
    before_time = before_time - 1.hour

    # Ignore hours before opening and after closing
    if (before_time < Time.beginning_of_workday(before_time))
      before_time = before_time - off_hours
    end

    # Ignore weekends and holidays
    while !Time.workday?(before_time)
      before_time = before_time - 1.day
    end
  end
  before_time
end

#from_nowObject


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

def from_now
  Time.zone ?  after(Time.zone.now) : after(Time.now)
end