Class: BankWorkingDay::Base

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(holidays_yml_path = '../../../holidays.yml') ⇒ Base

Returns a new instance of Base.



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

def initialize(holidays_yml_path='../../../holidays.yml')
  @holidays ||= Holidays.new(holidays_yml_path)
end

Instance Attribute Details

#holidaysObject

Returns the value of attribute holidays.



10
11
12
# File 'lib/bank_working_day.rb', line 10

def holidays
  @holidays
end

Instance Method Details

#deduction_date(year:, month:, day: 27) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/bank_working_day.rb', line 41

def deduction_date(year: , month: , day: 27)
  raise InvalidArgumentError if year.to_i.zero? || month.to_i.zero?

  date = Date.new(year.to_i, month.to_i, day.to_i)
  while holiday?(date)
    date += 1
  end

  date
end

#end_of_month_without_holiday(date) ⇒ Object



16
17
18
19
20
21
# File 'lib/bank_working_day.rb', line 16

def end_of_month_without_holiday(date)
  date.end_of_month.day.downto(1) do |last_day|
    last_day = Date.new(date.year, date.month, last_day)
    return last_day unless holiday?(last_day)
  end
end

#holiday?(date) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/bank_working_day.rb', line 52

def holiday?(date)
  holidays.holiday?(date)
end

#working_day_after(date:, offset:) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/bank_working_day.rb', line 32

def working_day_after(date: , offset: )
  offset.times do |i|
    date += 1
    date += 1 while holiday?(date)
  end

  date
end

#working_day_before(date:, offset:) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/bank_working_day.rb', line 23

def working_day_before(date: , offset: )
  offset.times do |i|
    date -= 1
    date -= 1 while holiday?(date)
  end

  date
end