# frozen_string_literal: true require 'json' require 'singleton' class Holiday attr_reader :holidays, :calendars def initialize(calendars) @calendars = calendars end def load_data @holidays = {} calendars.each do |calendar| file = File.expand_path( File.dirname(__FILE__) + "/../data/#{calendar}_holidays.json" ) raw_data = File.read(file) parsed = JSON.parse(raw_data) update_holidays(parsed) end end def in(year) load_data if holidays.nil? holidays[year] end private def update_holidays(parsed) parsed.map do |str| str.split('-') date = Date.parse(str) @holidays[date.year] ||= [] @holidays[date.year] << date end end end