Class: Antiquity

Inherits:
Object
  • Object
show all
Defined in:
lib/sinatra/extensions/antiquity.rb

Instance Method Summary collapse

Constructor Details

#initialize(periods) ⇒ Antiquity

Returns a new instance of Antiquity.



4
5
6
# File 'lib/sinatra/extensions/antiquity.rb', line 4

def initialize(periods)
  @periods = periods.sort { |a,b| a.start_date <=> b.start_date }
end

Instance Method Details

#days(upto_date = Date.today) ⇒ Object



8
9
10
# File 'lib/sinatra/extensions/antiquity.rb', line 8

def days(upto_date=Date.today)
  @periods.inject(0) { |total, period| total + period.days(upto_date) }
end

#months(upto_date = Date.today) ⇒ Object



12
13
14
# File 'lib/sinatra/extensions/antiquity.rb', line 12

def months(upto_date=Date.today)
  @periods.inject(0) { |total, period| total + period.months(upto_date) }
end

#to_s(upto_date = Date.today) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sinatra/extensions/antiquity.rb', line 20

def to_s(upto_date=Date.today)
  date = (@periods.last && @periods.last.end_date) || upto_date
  result = ""
  return result if @periods.empty?
  if date >= @periods.first.start_date
    years_upto_date = years(date)
    months_upto_date = months(date)
    result += years_upto_date > 1 ? "#{years_upto_date} años" : "#{years_upto_date} año" unless years_upto_date == 0
    result += ", " if years_upto_date >= 1 && !(months_upto_date.modulo(12).eql?(0))
    result += months_upto_date.modulo(12) > 1 ? "#{months_upto_date.modulo(12)} meses" : "#{months_upto_date.modulo(12)} mes" unless months_upto_date.modulo(12) == 0
    result += "#{(date - @periods.last.start_date).to_i} dias" if months_upto_date.modulo(12) == 0 && years_upto_date == 0 && date > @periods.last.start_date
    result += "Hoy!" if months_upto_date.modulo(12) == 0 && date == @periods.last.start_date
  end
  result += "En unos días..." if date < @periods.first.start_date
  result
end

#years(upto_date = Date.today) ⇒ Object



16
17
18
# File 'lib/sinatra/extensions/antiquity.rb', line 16

def years(upto_date=Date.today)
  months(upto_date).divmod(12).first
end