Class: Holidays::UseCase::Context::Between

Inherits:
Object
  • Object
show all
Defined in:
lib/holidays/use_case/context/between.rb

Instance Method Summary collapse

Constructor Details

#initialize(holidays_by_month_repo, day_of_month_calculator, proc_cache_repo) ⇒ Between

Returns a new instance of Between.



5
6
7
8
9
# File 'lib/holidays/use_case/context/between.rb', line 5

def initialize(holidays_by_month_repo, day_of_month_calculator, proc_cache_repo)
  @holidays_by_month_repo = holidays_by_month_repo
  @day_of_month_calculator = day_of_month_calculator
  @proc_cache_repo = proc_cache_repo
end

Instance Method Details

#call(start_date, end_date, dates_driver, regions, observed, informal) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/holidays/use_case/context/between.rb', line 11

def call(start_date, end_date, dates_driver, regions, observed, informal)
  validate!(start_date, end_date, dates_driver, regions)

  holidays = []

  dates_driver.each do |year, months|
    months.each do |month|
      next unless hbm = holidays_by_month_repo.find_by_month(month)
      hbm.each do |h|
        next unless in_region?(regions, h[:regions])

        # Skip informal holidays unless they have been requested
        next if h[:type] == :informal and not informal

        # range check feature.
        if h[:year_ranges]
          valid_range_year = false
          h[:year_ranges].each do |year_range|
            next unless year_range.is_a?(Hash) && year_range.length == 1
            next unless year_range.select{
              |operator,year|[:before,"before",:after,"after",:limited,"limited",:between,"between"].include?(operator)}.count > 0
            case year_range.keys.first
            when :before,"before"
              valid_range_year = true if year <= year_range[year_range.keys.first]
            when :after,"after"
              valid_range_year = true if year >= year_range[year_range.keys.first]
            when :limited,"limited"
              valid_range_year = true if year_range[year_range.keys.first].include?(year)
            when :between,"between"
              year_range[year_range.keys.first] = Range.new(*year_range[year_range.keys.first].split("..").map(&:to_i)) if year_range[year_range.keys.first].is_a?(String)
              valid_range_year = true if year_range[year_range.keys.first].cover?(year)
            end
            break if valid_range_year
          end
          next unless valid_range_year
        end

        if h[:function]
          # Holiday definition requires a calculation
          result = call_proc(h[:function], year)
          # Procs may return either Date or an integer representing mday
          if result.kind_of?(Date)
            month = result.month
            mday = result.mday
          else
            mday = result
          end
        else
          # Calculate the mday
          mday = h[:mday] || day_of_month_calculator.call(year, month, h[:week], h[:wday])
        end

        # Silently skip bad mdays
        begin
          date = Date.civil(year, month, mday)
        rescue; next; end

        # If the :observed option is set, calculate the date when the holiday
        # is observed.
        if observed and h[:observed]
          date = call_proc(h[:observed], date)
        end

        if date.between?(start_date, end_date)
          holidays << {:date => date, :name => h[:name], :regions => h[:regions]}
        end
      end
    end
  end

  holidays.sort{|a, b| a[:date] <=> b[:date] }
end