Class: Holidays::Finder::Context::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/holidays/finder/context/search.rb

Instance Method Summary collapse

Constructor Details

#initialize(holidays_by_month_repo, custom_method_processor, day_of_month_calculator, rules) ⇒ Search

Returns a new instance of Search.



5
6
7
8
9
10
# File 'lib/holidays/finder/context/search.rb', line 5

def initialize(holidays_by_month_repo, custom_method_processor, day_of_month_calculator, rules)
  @holidays_by_month_repo = holidays_by_month_repo
  @custom_method_processor = custom_method_processor
  @day_of_month_calculator = day_of_month_calculator
  @rules = rules
end

Instance Method Details

#call(dates_driver, regions, options) ⇒ Object



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
# File 'lib/holidays/finder/context/search.rb', line 12

def call(dates_driver, regions, options)
  validate!(dates_driver)

  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 if informal_type?(h[:type]) && !informal_set?(options)
        next unless @rules[:in_region].call(regions, h[:regions])

        if h[:year_ranges]
          next unless @rules[:year_range].call(year, h[:year_ranges])
        end

        dates = if h[:function]
          custom_holidays(year, month, h, regions)
        else
          date = build_date(year, month, h, regions)
          date ? [date] : []
        end

        dates.each do |d|
          if observed_set?(options) && h[:observed]
            d = build_observed_date(d, regions, h)
          end
          holidays << {:date => d, :name => h[:name], :regions => h[:regions]}
        end
      end
    end
  end

  holidays.sort_by.with_index do |h, i|
    direct = h[:regions].any? { |r| regions.include?(r) } ? 0 : 1
    [direct, i]
  end
end