Class: Date

Inherits:
Object show all
Defined in:
lib/ruby_units/date.rb

Overview

Allow date objects to do offsets by a time unit Date.today + U“1 week” => gives today+1 week

Instance Method Summary collapse

Instance Method Details

#+(unit) ⇒ Unit

Parameters:

Returns:



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

def +(unit)
  case unit
  when Unit
    unit = unit.convert_to('d').round if ['y', 'decade', 'century'].include? unit.units 
    unit_date_add(unit.convert_to('day').scalar)
  else
    unit_date_add(unit)
  end
end

#-(unit) ⇒ Unit

Parameters:

Returns:



22
23
24
25
26
27
28
29
30
# File 'lib/ruby_units/date.rb', line 22

def -(unit)
  case unit
  when Unit 
    unit = unit.convert_to('d').round if ['y', 'decade', 'century'].include? unit.units 
    unit_date_sub(unit.convert_to('day').scalar)
  else
    unit_date_sub(unit)
  end
end

#inspect(raw = false) ⇒ Object

Deprecated.


52
53
54
55
# File 'lib/ruby_units/date.rb', line 52

def inspect(raw = false)
  return self.units_datetime_inspect if raw
  self.to_s
end

#to_dateDate

:nocov_19:

Returns:



60
61
62
# File 'lib/ruby_units/date.rb', line 60

def to_date
  Date.civil(self.year, self.month, self.day)
end

#to_timeTime

Returns:



44
45
46
# File 'lib/ruby_units/date.rb', line 44

def to_time
  Time.local(*ParseDate.parsedate(self.to_s))
end

#to_unit(other = nil) ⇒ Unit Also known as: unit

Construct a unit from a Date

Examples:

Date.today.to_unit => Unit

Parameters:

  • other (Object) (defaults to: nil)

    convert to same units as passed

Returns:



36
37
38
# File 'lib/ruby_units/date.rb', line 36

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

#unit_date_addObject



6
# File 'lib/ruby_units/date.rb', line 6

alias :unit_date_add :+

#unit_date_subObject



19
# File 'lib/ruby_units/date.rb', line 19

alias :unit_date_sub :-

#units_datetime_inspectObject

:nocov_19:



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

alias :units_datetime_inspect :inspect