Module: RubyUnits::Date

Included in:
Date
Defined in:
lib/ruby_units/date.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#+(other) ⇒ RubyUnits::Unit

Allow date objects to do offsets by a time unit

Examples:

Date.today + Unit.new(“1 week”) => gives today+1 week

Parameters:

Returns:



11
12
13
14
15
16
17
18
19
# File 'lib/ruby_units/date.rb', line 11

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) ⇒ RubyUnits::Unit

Allow date objects to do offsets by a time unit

Examples:

Date.today - Unit.new(“1 week”) => gives today-1 week

Parameters:

Returns:



26
27
28
29
30
31
32
33
34
# File 'lib/ruby_units/date.rb', line 26

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) ⇒ Object

Deprecated.


47
48
49
# File 'lib/ruby_units/date.rb', line 47

def inspect(dump = false)
  dump ? super : to_s
end

#to_unit(other = nil) ⇒ RubyUnits::Unit

Construct a unit from a Date. This returns the number of days since the start of the Julian calendar as a Unit.

Examples:

Date.today.to_unit => Unit

Parameters:

Returns:



42
43
44
# File 'lib/ruby_units/date.rb', line 42

def to_unit(other = nil)
  other ? RubyUnits::Unit.new(self).convert_to(other) : RubyUnits::Unit.new(self)
end