Module: Snails::RelativeTime

Defined in:
lib/snails/util.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.minutes_in_words(min) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/snails/util.rb', line 7

def self.minutes_in_words(min)
  case min.to_i
    when 0..1            then 'un minuto'
    when 2..4            then '5 minutos'
    when 5..14           then '15 minutos'
    when 15..29          then "media hora"
    when 30..59          then "#{min} min"
    when 60..1439        then "#{(min/60).floor} horas"
    when 1440..11519     then "#{(min/1440).floor} días"
    when 11520..43199    then "#{(min/11520).floor} semanas"
    when 43200..525599   then "#{(min/43200).floor} meses"
    else                      "#{(min/525600).floor} años"
  end
end

Instance Method Details

#in_wordsObject



22
23
24
25
26
# File 'lib/snails/util.rb', line 22

def in_words
  minutes = (((Time.now - self).abs)/60).round
  return nil if minutes < 0
  RelativeTime.minutes_in_words(minutes)
end

#relativeObject



28
29
30
31
32
33
34
35
36
37
# File 'lib/snails/util.rb', line 28

def relative
  if str = in_words
    if Time.now < self
      # "#{str} más"
      "en #{str}"
    else
      "hace #{str}"
    end
  end
end