Method: Subunit#to_s

Defined in:
lib/subunit.rb

#to_s(omit: [], verbose: true) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/subunit.rb', line 73

def to_s(omit: [], verbose: true)
  
  if not verbose then
    
    r =  self.to_a.reverse.take_while {|x| x > 0}.reverse\
        .map {|x| "%02d" % x}.join(':')
    
    if r.length < 2 then
      return '00:00'
    elsif r.length == 2      
      return '00:' + r
    else
      return r
    end
    
  end
  
  h = @to_h
  omit.each {|x| h.delete x}

  list = h.to_a
  n = list.find {|_,v| v > 0 }
  a = list[list.index(n)..-1].map {|x|"%d %s" % x.reverse}        
      
  duration = case a.length

  when 1
    a.first  
  when 2
    a.join ' and '
  else                   
    "%s and %s" % [a[0..-2].join(', '), a[-1]]
  end
  
  duration.gsub(/(\b1 \w+)s/, '\1')

end