Sha256: 84bfd1954f2d1762ba06f2f8acb8914bd88a031162891601b679017daec332e5

Contents?: true

Size: 1.35 KB

Versions: 2

Compression:

Stored size: 1.35 KB

Contents

require "increments/schedule/version"
require 'active_support/all'
require 'holiday_japan'

module Increments
  module Schedule
    module_function

    [
      [:each_remote_work_day,         proc { |date| remote_work_day?(date) }],
      [:each_normal_remote_work_day,  proc { |date| normal_remote_work_day?(date) }],
      [:each_special_remote_work_day, proc { |date| special_remote_work_day?(date) }]
    ].each do |method_name, conditional|
      define_method(method_name) do |max_date = nil, &block|
        return to_enum(__method__, max_date) unless block

        max_date ||= Date.today + 1.year

        Date.today.upto(max_date) do |date|
          block.call(date) if conditional.call(date)
        end
      end
    end

    def remote_work_day?(date)
      normal_remote_work_day?(date) || special_remote_work_day?(date)
    end

    def normal_remote_work_day?(date)
      date.monday?
    end

    def special_remote_work_day?(date)
      office_work_day?(date) &&
        !office_work_day?(date.yesterday) && !office_work_day?(date.tomorrow)
    end

    def office_work_day?(date)
      !rest_day?(date) && !normal_remote_work_day?(date)
    end

    def rest_day?(date)
      weekend?(date) || holiday?(date)
    end

    def weekend?(date)
      date.saturday? || date.sunday?
    end

    def holiday?(date)
      HolidayJapan.check(date)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
increments-schedule-0.1.1 lib/increments/schedule.rb
increments-schedule-0.1.0 lib/increments/schedule.rb