Sha256: fe129de49bcb6c8a785959585cf1ee3dea1689fedfb336539c93f9afe61302aa

Contents?: true

Size: 746 Bytes

Versions: 3

Compression:

Stored size: 746 Bytes

Contents

# 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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
work_day-1.1.0 lib/holiday.rb
work_day-1.0.1 lib/holiday.rb
work_day-1.0.0 lib/holiday.rb