merged MethodChain and BackwardsCompatibility

modules into a single TimeExt::Support module
This commit is contained in:
2010-08-04 17:28:22 +03:00
parent e8d7d9d47b
commit 77d9d67ed1
5 changed files with 29 additions and 36 deletions

View File

@@ -1,8 +1,8 @@
require 'rubygems'
require 'active_support'
require 'time_ext/backwards_compatibility'
require 'time_ext/calculations'
require 'time_ext/iterations'
require 'time_ext/method_chain'
require 'time_ext/support'
require 'time_ext/core_ext/time'

View File

@@ -1,14 +0,0 @@
module TimeExt
# Provides helper methods used by TimeExt::Calculations for backwards compatibility with ActiveSupport.
module BackwardsCompatibility
def days_into_week
defined?(DAYS_INTO_WEEK) ? DAYS_INTO_WEEK : { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6 }
end
def common_year_days_in_month
defined?(COMMON_YEAR_DAYS_IN_MONTH) ? COMMON_YEAR_DAYS_IN_MONTH : [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
end
end
end

View File

@@ -1,6 +1,5 @@
class Time
include TimeExt::MethodChain
include TimeExt::BackwardsCompatibility
include TimeExt::Support
include TimeExt::Calculations
include TimeExt::Iterations

View File

@@ -1,19 +0,0 @@
module TimeExt
# Allows iterators' #until and #from methods to chain back to the parent iteration method.
module MethodChain
def add_to_chain(method, *args, &block)
@method_chain ||= []
@method_chain << [method.to_sym, args, block]
end
def call_chain(custom_block = nil, &block)
method, args, iblock = @method_chain.pop
return nil if method.nil?
iblock = custom_block if !custom_block.nil?
method, args, iblock = yield(method, args, iblock) if block_given?
self.send(method, *args, &iblock)
end
end
end

27
lib/time_ext/support.rb Normal file
View File

@@ -0,0 +1,27 @@
module TimeExt
# Provides helper methods used by TimeExt::Calculations for backwards compatibility with ActiveSupport, and method chaining helpers for TimeExt::Iterations.
module Support
def days_into_week
defined?(DAYS_INTO_WEEK) ? DAYS_INTO_WEEK : { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6 }
end
def common_year_days_in_month
defined?(COMMON_YEAR_DAYS_IN_MONTH) ? COMMON_YEAR_DAYS_IN_MONTH : [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
end
def add_to_chain(method, *args, &block)
@method_chain ||= []
@method_chain << [method.to_sym, args, block]
end
def call_chain(custom_block = nil, &block)
method, args, iblock = @method_chain.pop
return nil if method.nil?
iblock = custom_block if !custom_block.nil?
method, args, iblock = yield(method, args, iblock) if block_given?
self.send(method, *args, &iblock)
end
end
end