Sha256: 7bcefb2bd2e16c92753047346d1aa66538c34900ed01a34553d27757e2c4396f

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

module GBWorkDay
  class WorkWeek
    attr_reader :work_days_per_week, :free_days_per_week, :work_days, :free_days

    # @param work_days [#to_i] Amount of working days in a week. Default value is 7.
    def initialize(work_days = 7)
      work_days = work_days.to_i
      week_start = 1
      raise ArgumentError, 'Work days have to be between 0 and 7!' unless work_days >= 0 && work_days <= 7
      @work_days_per_week = work_days
      @week_start = week_start
      @free_days_per_week = 7 - @work_days_per_week
      @work_days = []
      @work_days_per_week.times do
        day = week_start % 7
        @work_days << (day != 0 ? day : 7)
        week_start += 1
      end
      @work_days.sort!
      @free_days = (1..7).to_a - @work_days
    end

    # Check if given day is a work day
    # @param day [Time, Date]
    def work_day?(day)
      week_day = day.wday
      week_day = 7 if week_day == 0
      work_days.include? week_day
    end

    # Check if given day is a work day
    # @param day [Time, Date]
    def free_day?(day)
      week_day = day.wday
      week_day = 7 if week_day == 0
      free_days.include? week_day
    end

    def ==(other) # :nodoc:
      work_days_per_week == other.work_days_per_week
    end

    def eql?(other) # :nodoc:
      work_days_per_week.eql?(other.work_days_per_week)
    end

    class << self
      def current
        Thread.current[:working_week] ||= self.new
      end

      def current=(new_week)
        Thread.current[:working_week] = new_week if WorkWeek === new_week
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gb_work_day-0.1.0 lib/gb_work_day/work_week.rb