Sha256: 41d02649e8a519fcb41e04901f607249c8ebb8cbe164956610fb0264639dcffe
Contents?: true
Size: 1.65 KB
Versions: 5
Compression:
Stored size: 1.65 KB
Contents
require 'date' module RubyUnits # Extra methods for [::Date] to allow it to be used as a [RubyUnits::Unit] module Date # Allow date objects to do offsets by a time unit # # @example Date.today + Unit.new("1 week") => gives today+1 week # @param [RubyUnits::Unit, Object] other # @return [RubyUnits::Unit] def +(other) case other when RubyUnits::Unit other = other.convert_to('d').round if %w[y decade century].include? other.units super(other.convert_to('day').scalar) else super end end # Allow date objects to do offsets by a time unit # # @example Date.today - Unit.new("1 week") => gives today-1 week # @param [RubyUnits::Unit, Object] other # @return [RubyUnits::Unit] def -(other) case other when RubyUnits::Unit other = other.convert_to('d').round if %w[y decade century].include? other.units super(other.convert_to('day').scalar) else super end end # Construct a unit from a Date. This returns the number of days since the # start of the Julian calendar as a Unit. # # @example Date.today.to_unit => Unit # @return [RubyUnits::Unit] # @param other [RubyUnits::Unit, String] convert to same units as passed def to_unit(other = nil) other ? RubyUnits::Unit.new(self).convert_to(other) : RubyUnits::Unit.new(self) end # @deprecated def inspect(dump = false) dump ? super : to_s end end end # @note Do this instead of Date.prepend(RubyUnits::Date) to avoid YARD warnings # @see https://github.com/lsegal/yard/issues/1353 class Date prepend RubyUnits::Date end
Version data entries
5 entries across 5 versions & 1 rubygems