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:



13
14
15
16
17
18
19
20
21
# 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) ⇒ 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:



28
29
30
31
32
33
34
35
36
# 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) ⇒ Object

Deprecated.


49
50
51
# File 'lib/ruby_units/date.rb', line 49

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:



44
45
46
# 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