Sha256: 13ac8edf70adfb6b7dbc9a5e4553f4ffffb72141f6fef20dfadd920cb7f21c5e

Contents?: true

Size: 1.11 KB

Versions: 7

Compression:

Stored size: 1.11 KB

Contents

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

require 'date'

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

 
  alias :unit_date_sub :-    
  def -(unit)
    case unit
    when Unit 
      unit = unit.to('d').round if ['y', 'decade', 'century'].include? unit.units 
      unit_date_sub(unit.to('day').scalar)
    else
      unit_date_sub(unit)
    end
  end
  
  def to_unit(other = nil)
    other ? Unit.new(self).to(other) : Unit.new(self)
  end
  alias :unit :to_unit
  
  unless Date.instance_methods.include?(:to_time)
    def to_time
      Time.local(*ParseDate.parsedate(self.to_s))
    end
  end
  
  alias :units_datetime_inspect :inspect
  def inspect(raw = false)
    return self.units_datetime_inspect if raw
    self.to_s
  end
  
  unless Date.instance_methods.include?(:to_date)
    def to_date
      Date.civil(self.year, self.month, self.day)
    end
  end
  
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
rails-units-1.7.1 lib/rails_units/date.rb
rails-units-1.6.0 lib/rails_units/date.rb
rails-units-1.5.2 lib/rails_units/date.rb
rails-units-1.5.1 lib/rails_units/date.rb
rails-units-1.4.1 lib/ruby_units/date.rb
rails-units-1.3.1 lib/ruby_units/date.rb
ruby-units-1.3.1 lib/ruby_units/date.rb