Sha256: 41a1144bf0dc36c8e29367b496a880b928edd7bcd107ec5991b14f632410a0a6

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

require 'increments/schedule/version'
require 'holiday_japan'

module Increments
  module Schedule
    extend self # rubocop:disable ModuleFunction

    [
      [:each_remote_work_day,         :remote_work_day?],
      [:each_normal_remote_work_day,  :normal_remote_work_day?],
      [:each_special_remote_work_day, :special_remote_work_day?]
    ].each do |enumeration_method, predicate_method|
      define_method(enumeration_method) do |max_date = nil, &block|
        return to_enum(__method__, max_date) unless block

        max_date ||= Date.today + 365

        Date.today.upto(max_date) do |date|
          block.call(date) if send(predicate_method, 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? && !rest_day?(date)
    end

    def special_remote_work_day?(date)
      normal_office_work_day?(date) &&
        !normal_office_work_day?(date - 1) && !normal_office_work_day?(date + 1)
    end

    def office_work_day?(date)
      normal_office_work_day?(date) && !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

    private

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
increments-schedule-0.1.2 lib/increments/schedule.rb