weekdays

对数字类加上 日期 的扩展而矣。

这是示例:

Date.today.weekday? # => true
5.weekdays_ago # => Tue Mar 24 10:46:42 -0400 2009
11.weekdays_from_now # => Wed Apr 15 10:46:54 -0400 2009
20.weekdays_from(Date.yesterday) # => Mon, 27 Apr 2009
Date.today.weekdays_until(Date.tomorrow) # => 1

下面这是全部源代码(总共就 4 个方法)。

module ActiveSupport #:nodoc:
  module CoreExtensions #:nodoc:
    module Date #:nodoc:
      # Enables the use of time calculations within Date itself
      module Calculations
        # Tells whether the Date object is a weekday
        def weekday?
          (1..5).include?(wday)
        end

        # Returns the number of weekdays until a future Date
        def weekdays_until(date)
          return 0 if date <= self
          (self...date).select{|day| day.weekday?}.size
        end
      end
    end
  end
end

class Date
  include ActiveSupport::CoreExtensions::Date::Calculations
end

module ActiveSupport #:nodoc:
  module CoreExtensions #:nodoc:
    module Time #:nodoc:
      # Enables the use of time calculations within Time itself
      module Calculations
        def weekday?
          (1..5).include?(wday)
        end

        def weekdays_until(date)
          return 0 if date <= self.to_date
          (self.to_date...date).select{|day| day.weekday?}.size
        end
      end
    end
  end
end

class Time
  include ActiveSupport::CoreExtensions::Time::Calculations
end

module ActiveSupport #:nodoc:
  module CoreExtensions #:nodoc:
    module DateTime #:nodoc:
      # Enables the use of time calculations within DateTime itself
      module Calculations
        # Tells whether the Date object is a weekday
        def weekday?
          (1..5).include?(wday)
        end
      end
    end
  end
end

class Time
  include ActiveSupport::CoreExtensions::DateTime::Calculations
end

module ActiveSupport #:nodoc:
  module CoreExtensions
    module Numeric
      module Time
        # Returns a Time object that is n number of weekdays in the future of a given Date
        def weekdays_from(time = ::Time.now)
          # -5.weekdays_from(time) == 5.weekdays_ago(time)
          return self.abs.weekdays_ago(time) if self < 0

          x = 0
          curr_date = time

          until x == self
            curr_date += 1.days
            x += 1 if curr_date.weekday?
          end

          curr_date
        end
        alias :weekdays_from_now :weekdays_from

        # Returns a Time object that is n number of weekdays in the past from a given Date
        def weekdays_ago(time = ::Time.now)
          # -5.weekdays_ago(time) == 5.weekdays_from(time)
          return self.abs.weekdays_from(time) if self < 0

          x = 0
          curr_date = time

          until x == self
            curr_date -= 1.days
            x += 1 if curr_date.weekday?
          end

          curr_date
        end
      end
    end
  end
end

class Numeric
  include ActiveSupport::CoreExtensions::Numeric::Time
end

示例里有五个方法,怎么才有 4 个def ???
认真看点,这里有一行:

  alias :weekdays_from_now :weekdays_from

你可能感兴趣的:(weekdays)