Class: Holidays::Finder::Rules::YearRange

Inherits:
Object
  • Object
show all
Defined in:
lib/holidays/finder/rules/year_range.rb

Constant Summary collapse

BEFORE =
:before
AFTER =
:after
LIMITED =
:limited
BETWEEN =
:between
VALID_OPERATORS =

TODO Can we just accept symbols here? Why accept strings?

[
  BEFORE, BEFORE.to_s,
  AFTER, AFTER.to_s,
  LIMITED, LIMITED.to_s,
  BETWEEN, BETWEEN.to_s
]

Class Method Summary collapse

Class Method Details

.call(target_year, year_range_definitions) ⇒ Object



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
# File 'lib/holidays/finder/rules/year_range.rb', line 21

def call(target_year, year_range_definitions)
  validate!(target_year, year_range_definitions)

  matched = false
  year_range_definitions.each do |range_defs|
    next unless range_defs.is_a?(Hash) && range_defs.length == 1

    operator = range_defs.keys.first
    year_range = range_defs.values.first

    case operator
    when BEFORE, BEFORE.to_s
      matched = target_year <= year_range
    when AFTER, AFTER.to_s
      matched = target_year >= year_range
    when LIMITED, LIMITED.to_s
      if year_range.is_a?(Array)
        matched = year_range.include?(target_year)
      else
        matched = year_range == target_year
      end
    when BETWEEN, BETWEEN.to_s
      matched = year_range.cover?(target_year)
    end

    break if matched == true
  end

  matched
end