Sha256: b06851e382d78373f5d650f3d4b50173845d45e6650a790c3992a86a9418ab51

Contents?: true

Size: 1000 Bytes

Versions: 2

Compression:

Stored size: 1000 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)
      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.4.0 lib/russian_workdays.rb
russian_workdays-1.2.0 lib/russian_workdays.rb