Class: BusinessTime::BusinessDays

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/business_time/business_days.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(days) ⇒ BusinessDays

Returns a new instance of BusinessDays.



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

def initialize(days)
  @days = days
end

Instance Attribute Details

#daysObject (readonly)

Returns the value of attribute days.



6
7
8
# File 'lib/business_time/business_days.rb', line 6

def days
  @days
end

Instance Method Details

#<=>(other) ⇒ Object



12
13
14
15
16
17
# File 'lib/business_time/business_days.rb', line 12

def <=>(other)
  if other.class != self.class
    raise ArgumentError.new("#{self.class} can't be compared with #{other.class}")
  end
  self.days <=> other.days
end

#after(time = Time.current) ⇒ Object Also known as: from_now, since



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/business_time/business_days.rb', line 19

def after(time = Time.current)
  days = @days
  while days > 0 || !time.workday?
    days -= 1 if time.workday?
    time += 1.day
  end
  # If we have a Time or DateTime object, we can roll_forward to the
  #   beginning of the next business day
  if time.is_a?(Time) || time.is_a?(DateTime)
    time = Time.roll_forward(time) unless time.during_business_hours?
  end
  time
end

#before(time = Time.current) ⇒ Object Also known as: ago, until



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

def before(time = Time.current)
  days = @days
  while days > 0 || !time.workday?
    days -= 1 if time.workday?
    time -= 1.day
  end
  # If we have a Time or DateTime object, we can roll_backward to the
  #   beginning of the previous business day
  if time.is_a?(Time) || time.is_a?(DateTime)
    unless time.during_business_hours?
      time = Time.beginning_of_workday(Time.roll_backward(time))
    end
  end
  time
end