Sha256: 36eda3799360d6e2b14f40a5172994a0eb4a13d116030a3d5122332f79fda1b4

Contents?: true

Size: 1005 Bytes

Versions: 2

Compression:

Stored size: 1005 Bytes

Contents

require 'yaml'

class RussianWorkdays
  class << self
    %w(holiday short).each do |method|
      define_method("#{method}?") do |date|
        raise ArgumentError.new('Argument must be Date object') unless date.is_a?(Date)
        dates[date.year][method].include?(date.to_s)
      end

      define_method("#{method}s") do |year|
        has_data_for_year?(year)
        dates[year][method]
      end
    end

    def work?(date)
      raise ArgumentError unless date.is_a?(Date)
      !holidays(date.year).include?(date) && !shorts(date.year).include?(date)
    end

    def works(year)
      has_data_for_year?(year)
      (Date.new(year, 1, 1)..Date.new(year, 12, 31)).to_a - holidays(year) - shorts(year)
    end

    private
      def has_data_for_year?(year)
        raise ArgumentError.new('Data missing for that year') unless dates.keys.include?(year)
      end

      def dates
        @@dates ||= YAML.load_file(File.expand_path('../russian_workdays/dates.yml', __FILE__))
      end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
russian_workdays-1.1.0 lib/russian_workdays.rb
russian_workdays-1.0.0 lib/russian_workdays.rb