class SwedishHolidays::Holiday

Attributes

date[R]
name[R]

Public Class Methods

each(year = Date.today.year, real: no_arg, include_informal: no_arg, &block) click to toggle source
# File lib/swedish_holidays/holiday.rb, line 29
def each(year = Date.today.year, real: no_arg, include_informal: no_arg, &block)
  informal = Utils.include_informal?(real, include_informal)
  load(year)
  holidays = loaded[year.to_i].values
  holidays.delete_if { |holiday| !informal && holiday.informal? }
  return holidays.each(&block) if block

  holidays.each
end
find(date, real: no_arg, include_informal: no_arg) click to toggle source
# File lib/swedish_holidays/holiday.rb, line 20
def find(date, real: no_arg, include_informal: no_arg)
  informal = Utils.include_informal?(real, include_informal)
  year = date.year
  load(year)
  holiday = loaded[year][date.yday]
  return holiday if informal
  return holiday if holiday&.real?
end
holiday?(date, real: no_arg, include_informal: no_arg) click to toggle source
# File lib/swedish_holidays/holiday.rb, line 15
def holiday?(date, real: no_arg, include_informal: no_arg)
  informal = Utils.include_informal?(real, include_informal)
  !find(date, include_informal: informal).nil?
end
new(attr) click to toggle source
# File lib/swedish_holidays/holiday.rb, line 69
def initialize(attr)
  @date = Date.parse(attr[:date])
  @name = attr[:name]
  @informal = attr.fetch(:informal_holiday) { !attr[:real_holiday] }
end

Private Class Methods

file(year) click to toggle source
# File lib/swedish_holidays/holiday.rb, line 45
def file(year)
  File.join(DATA_DIR, year.to_s)
end
load(year) click to toggle source
# File lib/swedish_holidays/holiday.rb, line 49
def load(year)
  return unless loaded[year.to_i].empty?
  validate_has_year! year
  holidays = YAML.load_file file(year.to_s)
  holidays.each do |hash|
    holiday = new(hash)
    loaded[year.to_i][holiday.yday] = holiday
  end
end
loaded() click to toggle source
# File lib/swedish_holidays/holiday.rb, line 64
def loaded
  @loaded ||= Hash.new { |hash, key| hash[key] = {} }
end
no_arg() click to toggle source
# File lib/swedish_holidays/holiday.rb, line 41
def no_arg
  Utils::VALUE_NOT_GIVEN
end
validate_has_year!(year) click to toggle source
# File lib/swedish_holidays/holiday.rb, line 59
def validate_has_year!(year)
  err_msg = "Sorry I don't have data for year \"#{year}\""
  raise Error, err_msg unless File.exist? file(year)
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/swedish_holidays/holiday.rb, line 77
def <=>(other)
  date <=> other.date
end
informal?() click to toggle source
# File lib/swedish_holidays/holiday.rb, line 89
def informal?
  @informal
end
real?() click to toggle source
# File lib/swedish_holidays/holiday.rb, line 85
def real?
  !informal?
end
to_s() click to toggle source
# File lib/swedish_holidays/holiday.rb, line 81
def to_s
  "#{date.strftime}: #{name}"
end