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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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 (h[:type] == :informal) && !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

        if h[:function]
          result = @custom_method_processor.call(
            year, month, h[:mday],
            h[:function], h[:function_arguments], h[:function_modifier],
          )

          #FIXME The result should always be present, see https://github.com/holidays/holidays/issues/204 for more information
          if result
            month = result.month
            mday = result.mday
          end
        else
          mday = h[:mday] || @day_of_month_calculator.call(year, month, h[:week], h[:wday])
        end

        # Silently skip bad mdays
        #TODO Should we be doing something different here? We have no concept of logging right now. Maybe we should add it?
        begin
          date = Date.civil(year, month, mday)
        rescue; next; end

        if observed_set?(options) && h[:observed]
          date = @custom_method_processor.call(
            date.year, date.month, date.day,
            h[:observed],
            [:date],
          )
        end

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

  holidays
end