Sha256: ed61d5052861c23c2064b53d1b13daf8dc1f41c53ed67ab4a04f49febcdb63d2

Contents?: true

Size: 1.77 KB

Versions: 1

Compression:

Stored size: 1.77 KB

Contents

require 'gb_work_day/duration'
require 'gb_work_day/helpers/date'
class Date
  def plus_with_work_duration(other)
    if GBWorkDay::Duration === other
      other.since(self)
    else
      plus_without_work_duration(other)
    end
  end
  alias_method :plus_without_work_duration, :+
  alias_method :+, :plus_with_work_duration

  def minus_with_work_duration(other)
    if GBWorkDay::Duration === other
      plus_with_work_duration(- other)
    elsif GBWorkDay::Date === other
      - (other - self)
    else
      minus_without_work_duration(other)
    end
  end
  alias_method :minus_without_work_duration, :-
  alias_method :-, :minus_with_work_duration

  # Check if it is a work day.
  # @return [boolean]
  def work?(week = default_week)
    week.work_day? self
  end

  # Check if it is a work day.
  # @return [boolean]
  def free?(week = default_week)
    week.free_day? self
  end

  # Return next working day
  # @return [Time]
  def next_work_day(week = default_week)
    if week.free_day? self
      self.beginning_of_week + 7.days
    else
      self + GBWorkDay::Duration.new(1, week)
    end
  end

  # Return previous working day
  # @return [Time]
  def previous_work_day(week = default_week)
    if week.free_day? self
      next_work_day(week) - (week.free_days_per_week + 1).days
    else
      self - GBWorkDay::Duration.new(1, week)
    end
  end

  # Get date object for calculating working days
  #
  # @param week [GBWorkDay::WorkWeek] if not set, it will use week set globally. For more check {GBWorkingDay::WorkWeek#current}
  def work_date(week = nil)
    GBWorkDay::Date.from_date self, week
  end
  alias_method :to_work, :work_date
  alias_method :to_work_date, :work_date

  private

  # @return [GBWorkDay::WorkWeek]
  def default_week
    GBWorkDay::WorkWeek.current
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gb_work_day-0.1.0 lib/gb_work_day/core_ext/date.rb