Sha256: ac7357bddfb797d8bcc672bec536c668005b3bf1486c411ab1e79e43e00f9864

Contents?: true

Size: 1.16 KB

Versions: 3

Compression:

Stored size: 1.16 KB

Contents

module RichUnits

  # = Weekdays
  #
  # The Weekdays class provides useful weekday terminology.

  class Weekdays

    WEEKDAYS = 1..5 # Monday is wday 1
    ONE_DAY  = 60 * 60 * 24

    def initialize(n)
      @n = n
    end

    def ago(time = ::Time.now)
      step :down, time
    end
    alias_method :until, :ago
    alias_method :before, :ago

    def since(time = ::Time.now)
      step :up, time
    end
    alias_method :from_now, :since
    alias_method :after, :since

    private

    def step(direction, original_time)
      result = original_time
      time = ONE_DAY

      compare = direction == :up ? ">" : "<"
      time *= -1 if direction == :down

      @n.times do
        result += time until result.send(compare, original_time) && WEEKDAYS.member?(result.wday)
        original_time = result
      end
      result
    end

    # = Numeric Weekday Extensions
    #
    module Numeric

      # Works with day in terms of weekdays.
      def weekdays
        Weekdays.new(self)
      end

      alias_method :weekday, :weekdays

    end#module Numeric

  end#class Weekdays

end#module RichUnits


class Numeric #:nodoc:
  include RichUnits::Weekdays::Numeric
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
richunits-0.6.1 lib/richunits/weekdays.rb
richunits-0.6 lib/richunits/weekdays.rb
richunits-0.5.0 lib/richunits/weekdays.rb