Class: Subunit

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

Overview

file: subunit.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_units = nil, raw_subunit = 0) ⇒ Subunit

Returns a new instance of Subunit.



9
10
11
# File 'lib/subunit.rb', line 9

def initialize(raw_units=nil, raw_subunit=0)
  method((raw_units.class.to_s.downcase + '_units').to_sym).call(raw_units, raw_subunit)
end

Instance Attribute Details

#to_aObject (readonly)

Returns the value of attribute to_a.



7
8
9
# File 'lib/subunit.rb', line 7

def to_a
  @to_a
end

#to_hObject (readonly)

Returns the value of attribute to_h.



7
8
9
# File 'lib/subunit.rb', line 7

def to_h
  @to_h
end

Instance Method Details

#to_s(omit: []) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/subunit.rb', line 13

def to_s(omit: [])
  
  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