Class: Subunit

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_units = {}, obj) ⇒ Subunit

Returns a new instance of Subunit.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/subunit.rb', line 32

def initialize(raw_units={}, obj)
  
  @debug = false
  
  @raw_units = raw_units
      
  accumulate = ->(a) {
  
    len = raw_units.to_a.length
    val = ([0]*(len-1) + a).slice(-(len), len).zip(raw_units.values)\
        .inject(0) {|r,x| r * x[1] + x[0] }    
  }
  
  if obj.is_a? String
    
    @to_i = accumulate.call obj.split(/\D+/).map(&:to_i)
        
  elsif obj.is_a? Array then
          
    @to_i = accumulate.call obj
    
  else
    
    raw_subunit = obj || 0
    method((raw_units.class.to_s.downcase + '_units').to_sym)\
        .call(raw_units, raw_subunit)
    
  end
end

Instance Attribute Details

#to_aObject (readonly)

Returns the value of attribute to_a.



30
31
32
# File 'lib/subunit.rb', line 30

def to_a
  @to_a
end

#to_hObject (readonly)

Returns the value of attribute to_h.



30
31
32
# File 'lib/subunit.rb', line 30

def to_h
  @to_h
end

#to_iObject (readonly)

Returns the value of attribute to_i.



30
31
32
# File 'lib/subunit.rb', line 30

def to_i
  @to_i
end

Class Method Details

.hms_to_seconds(obj) ⇒ Object

e.g. Subunit.hms_to_seconds(‘5 minutes and 4 seconds’)

> 304



26
27
28
# File 'lib/subunit.rb', line 26

def self.hms_to_seconds(obj)
  new(units={hours:60, minutes:60,seconds: 60, }, obj).to_i
end

.seconds(val, units: nil) ⇒ Object



19
20
21
# File 'lib/subunit.rb', line 19

def self.seconds(val, units: nil)
  new(units={minutes:60, hours:60, days:24}, seconds: val)
end

Instance Method Details

#strfunit(s) ⇒ Object

usage: Subunit.new(units=hours:60, seconds: 661)\

.strfunit("%x") #=> 11m 1s

%x e.g. 11m 1s %xi e.g. 11m %X e.g. 11 minutes 1 second %Xi e.g. 11 minutes %c e.g. 00:11:01 %s e.g. 11m # returns the 1st most significant value while

ignoring the remainder


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
110
111
112
113
114
115
116
# File 'lib/subunit.rb', line 73

def strfunit(s)    
  
  # The i switch postfixed to the x or X switch will format the output 
  # without seconds unless there are no minutes.
  
  s.sub!(/%xi?/) do |x|
    
    a = to_h.to_a
    a2 = (a.length > 1 and x[-1][/i$/]) ? a[0..-2] : a
    a2.map {|label, val| val.to_s + label[0]}.join(' ')
    
  end
  
  s.sub!(/%Xi?/) do |x|

    a = to_h.to_a
    a2 = (a.length > 1 and x[-1][/i$/]) ? a[0..-2] : a      
    
    a2.map do |label, val| 
      
      next if val == 0
      label2 = val > 1 ? label.to_s : label.to_s.sub(/s$/,'')
      val.to_s + ' ' + label2
      
    end.compact.join(' ')
    
  end    
  
  s.sub!('%c') do |x|
    
    a = @raw_units.values.reverse
    a << a[-1]
    
    fmt = a.map {|v| "%0" + v.to_s.length.to_s + "d"}.join(":")
    fmt % to_a
  end    
  
  s.sub!('%s') do |x|
    label, val = to_h.first 
    val.to_s + label[0]
  end    

  s
end

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



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/subunit.rb', line 118

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