diff options
Diffstat (limited to 'activesupport/lib/active_support/core_ext/date/calculations.rb')
-rw-r--r-- | activesupport/lib/active_support/core_ext/date/calculations.rb | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/date/calculations.rb b/activesupport/lib/active_support/core_ext/date/calculations.rb new file mode 100644 index 0000000000..f81cd9187e --- /dev/null +++ b/activesupport/lib/active_support/core_ext/date/calculations.rb @@ -0,0 +1,41 @@ +module ActiveSupport #:nodoc: + module CoreExtensions #:nodoc: + module Date #:nodoc: + # Enables the use of time calculations within Time itself + module Calculations + def self.included(base) #:nodoc: + base.send(:include, ClassMethods) + + base.send(:alias_method, :plus_without_duration, :+) + base.send(:alias_method, :+, :plus_with_duration) + + base.send(:alias_method, :minus_without_duration, :-) + base.send(:alias_method, :-, :minus_with_duration) + end + + module ClassMethods + def plus_with_duration(other) #:nodoc: + if ActiveSupport::Duration === other + other.since(self) + else + plus_without_duration(other) + end + end + + def minus_with_duration(other) #:nodoc: + self.plus_with_duration(-other) + end + + # Provides precise Date calculations for years, months, and days. The +options+ parameter takes a hash with + # any of these keys: :months, :days, :years. + def advance(options) + d = ::Date.new(year + (options.delete(:years) || 0), month, day) + d = d >> options.delete(:months) if options[:months] + d = d + options.delete(:days) if options[:days] + d + end + end + end + end + end +end
\ No newline at end of file |