mirror of
https://github.com/jimeh/time_ext.git
synced 2026-02-19 05:16:40 +00:00
merged MethodChain and BackwardsCompatibility
modules into a single TimeExt::Support module
This commit is contained in:
@@ -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'
|
||||
|
||||
@@ -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
|
||||
@@ -1,6 +1,5 @@
|
||||
class Time
|
||||
include TimeExt::MethodChain
|
||||
include TimeExt::BackwardsCompatibility
|
||||
include TimeExt::Support
|
||||
include TimeExt::Calculations
|
||||
include TimeExt::Iterations
|
||||
|
||||
|
||||
@@ -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
27
lib/time_ext/support.rb
Normal 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
|
||||
Reference in New Issue
Block a user