Class: Duration

Inherits:
Object
  • Object
show all
Defined in:
lib/chronos/durationtotext.rb

Constant Summary collapse

ToTextEn =
{
  -(1/0.0) => proc { |s| sprintf "%d weeks ago", -s.div(604800) },
  -1209600 => proc { |s| "one week ago" },
  -604800 => proc { |s| sprintf "%d days ago", -s.div(86500) },
  -172800 => proc { |s| "yesterday" },
  0 => proc { |s| "today" },
  86400 => proc { |s| "tomorrow" },
  172800 => proc { |s| sprintf "in %d days", (s/86400).ceil },
  604800 => proc { |s| "in one week" },
  1209600 => proc { |s| sprintf "in %d weeks", (s/604800).ceil },
}.to_a.sort

Instance Method Summary collapse

Constructor Details

#initialize(seconds, months = 0) ⇒ Duration

Returns a new instance of Duration.



27
28
29
30
# File 'lib/chronos/durationtotext.rb', line 27

def initialize(seconds, months=0)
  @seconds = seconds
  @months  = months
end

Instance Method Details

#to_textObject Also known as: to_s



32
33
34
35
36
37
# File 'lib/chronos/durationtotext.rb', line 32

def to_text
  ToTextEn.each_cons(2) { |(v,t), (v2,t2)|
    return t.call(@seconds) if @seconds >= v && @seconds < v2
  }
  ToTextEn.last.last.call(@seconds)
end