Method: Credential.humanize_duration_from_seconds

Defined in:
app/models/credential.rb

.humanize_duration_from_seconds(seconds) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/models/credential.rb', line 43

def self.humanize_duration_from_seconds(seconds)
  # I did it this way so we can test easier
  until_citation = [
    (seconds/3600).floor,
    ((seconds % 3600)/60).floor,
    (seconds % 60)
  ]

  {0 => 'hour', 1 => 'minute', 2 => 'second'}.each_pair do |i, unit|
    until_citation[i] = '%d %s' % [
      until_citation[i], 
      ((until_citation[i] > 1) ? unit.pluralize : unit)
    ] if until_citation[i] and until_citation[i] > 0
  end
  
  until_citation.reject!{|i| i.nil? or i == 0 }
  
  until_citation.join ' '
end