module RubyUnits::Date

Extra methods for [::Date] to allow it to be used as a [RubyUnits::Unit]

Public Instance Methods

+(other) click to toggle source

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]

Calls superclass method
# File lib/ruby_units/date.rb, line 13
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
-(other) click to toggle source

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]

Calls superclass method
# File lib/ruby_units/date.rb, line 28
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
inspect(dump = false) click to toggle source

@deprecated

Calls superclass method
# File lib/ruby_units/date.rb, line 49
def inspect(dump = false)
  dump ? super : to_s
end
to_unit(other = nil) click to toggle source

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

# File lib/ruby_units/date.rb, line 44
def to_unit(other = nil)
  other ? RubyUnits::Unit.new(self).convert_to(other) : RubyUnits::Unit.new(self)
end