Class: BusinessDaysInstance

Inherits:
Object
  • Object
show all
Defined in:
lib/business-days.rb

Instance Method Summary collapse

Constructor Details

#initialize(holidays) ⇒ BusinessDaysInstance

Returns a new instance of BusinessDaysInstance.



159
160
161
162
163
164
165
166
# File 'lib/business-days.rb', line 159

def initialize(holidays)
  @holidays = []

  holidays.each do |holiday|
    # Include holiday
    @holidays.push(Date.parse(holiday))
  end
end

Instance Method Details

#business_day?(date) ⇒ Boolean

Check if the given date is a business day

Parameters:

  • date (Date)

    the non-business date to be add.

Returns:

  • (Boolean)

    true if the given date is a business day, false otherwise.

Raises:

  • (ArgumentError)


172
173
174
175
176
177
# File 'lib/business-days.rb', line 172

def business_day?(date)
  raise ArgumentError.new('Not a date') unless date.kind_of?(Date)

  weekend = [6, 7].include?(date.cwday)
  !(weekend || @holidays.include?(date))
end

#business_days(from, to) ⇒ Integer

Returns number of business days between the two dates.

Returns:

  • (Integer)

    number of business days between the two dates

Raises:

  • (ArgumentError)


185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/business-days.rb', line 185

def business_days(from, to)
  raise ArgumentError.new('Not a date: from') unless from.kind_of?(Date)
  raise ArgumentError.new('Not a date: to') unless to.kind_of?(Date)
  count = 0

  while  from <= to
    count +=1 if self.business_day?(from)
    from = from + 1.day
  end

  count
end