lib/holiday.rb in work_day-0.3.1 vs lib/holiday.rb in work_day-1.0.0
- old
+ new
@@ -1,34 +1,41 @@
-require "json"
-require "singleton"
+# frozen_string_literal: true
+require 'json'
+require 'singleton'
+
class Holiday
- include Singleton
- attr_reader :holidays
+ attr_reader :holidays, :calendars
- def self.load_data
- instance.load_data
+ def initialize(calendars)
+ @calendars = calendars
end
def load_data
@holidays = {}
- file = File.expand_path(File.dirname(__FILE__) + "/../data/holidays.json")
- raw_data = File.read(file)
- parsed = JSON.parse(raw_data)
+ 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|
- splitted = str.split("-")
+ str.split('-')
date = Date.parse(str)
@holidays[date.year] ||= []
@holidays[date.year] << date
end
-
- @holidays
- end
-
- def self.in(year)
- load_data if instance.holidays.nil?
- holidays = instance.holidays[year]
- holidays
end
end