class BusinessTime::BusinessDays

Attributes

days[R]

Public Class Methods

new(days, options={}) click to toggle source
# File lib/business_time/business_days.rb, line 8
def initialize(days, options={})
  @days = days
end

Public Instance Methods

<=>(other) click to toggle source
# 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, options={}) click to toggle source
# File lib/business_time/business_days.rb, line 19
def after(time = Time.current, options={})
  non_negative_days? ? calculate_after(time, @days, options) : calculate_before(time, -@days, options)
end
Also aliased as: from_now, since
ago(time = Time.current, options={})
Alias for: before
before(time = Time.current, options={}) click to toggle source
# File lib/business_time/business_days.rb, line 26
def before(time = Time.current, options={})
  non_negative_days? ? calculate_before(time, @days, options) : calculate_after(time, -@days, options)
end
Also aliased as: ago, until
from_now(time = Time.current, options={})
Alias for: after
since(time = Time.current, options={})
Alias for: after
until(time = Time.current, options={})
Alias for: before

Private Instance Methods

calculate_after(time, days, options={}) click to toggle source
# File lib/business_time/business_days.rb, line 39
def calculate_after(time, days, options={})
  if (time.is_a?(Time) || time.is_a?(DateTime)) && !time.workday?(options)
    time = Time.beginning_of_workday(time)
  end
  while days > 0 || !time.workday?(options)
    days -= 1 if time.workday?(options)
    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, options) unless time.during_business_hours?
  end
  time
end
calculate_before(time, days, options={}) click to toggle source
# File lib/business_time/business_days.rb, line 55
def calculate_before(time, days, options={})
  if (time.is_a?(Time) || time.is_a?(DateTime)) && !time.workday?(options)
    time = Time.beginning_of_workday(time)
  end
  while days > 0 || !time.workday?(options)
    days -= 1 if time.workday?(options)
    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, options))
    end
  end
  time
end
non_negative_days?() click to toggle source
# File lib/business_time/business_days.rb, line 35
def non_negative_days?
  @days >= 0
end