Sha256: 8e01252ec0390e960e3528f318bc466bc169f4f9e3545a938bc9bc28aa23bb6b

Contents?: true

Size: 1.11 KB

Versions: 5

Compression:

Stored size: 1.11 KB

Contents

# encoding: utf-8
class Antiquity

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

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

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

  def years
    months.divmod(12).first
  end

  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
      result += years > 1 ? "#{years} años" : "#{years} año" unless years == 0
      result += ", " if years >= 1 && !(months.modulo(12).eql?(0))
      result += months.modulo(12) > 1 ? "#{months.modulo(12)} meses" : "#{months.modulo(12)} mes" unless months.modulo(12) == 0
      result += "#{(date - @periods.last.start_date).to_i} dias" if months.modulo(12) == 0 && years == 0 && date > @periods.last.start_date
      result += "Hoy!" if months.modulo(12) == 0 && date == @periods.last.start_date
    end
    result += "En unos días..." if date < @periods.first.start_date
    result
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
sinatra-hexacta-0.9.12 lib/sinatra/extensions/antiquity.rb
sinatra-hexacta-0.9.11 lib/sinatra/extensions/antiquity.rb
sinatra-hexacta-0.9.10 lib/sinatra/extensions/antiquity.rb
sinatra-hexacta-0.9.9 lib/sinatra/extensions/antiquity.rb
sinatra-hexacta-0.9.8 lib/sinatra/extensions/antiquity.rb