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
# 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

        date = build_date(year, month, h)
        next unless date

        if observed_set?(options) && h[:observed]
          date = build_observed_date(date, regions, h)
        end

        holidays << {:date => date, :name => h[:name], :regions => h[:regions]}
      end
    end
  end

  holidays
end