Sha256: 53332b66bb32bf02081b5c0922f532188206637d907174c6af5844a1f279fc88

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

module RichUnits

  # = Weekdays
  #
  # The Weekdays class provides useful weekday terminology.
  #
  # Thanks to Dave Hoover and Ryan Platte for the Weekdays implementation.
  #
  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

1 entries across 1 versions & 1 rubygems

Version Path
richunits-0.6.2 lib/richunits/weekdays.rb