Class: Subunit
- Inherits:
-
Object
- Object
- Subunit
- Defined in:
- lib/subunit.rb
Instance Attribute Summary collapse
-
#to_a ⇒ Object
readonly
Returns the value of attribute to_a.
-
#to_h ⇒ Object
readonly
Returns the value of attribute to_h.
-
#to_i ⇒ Object
readonly
Returns the value of attribute to_i.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(raw_units = {}, obj) ⇒ Subunit
constructor
A new instance of Subunit.
-
#strfunit(s) ⇒ Object
usage: Subunit.new(units=hours:60, seconds: 661)\ .strfunit(“%x”) #=> 11m 1s.
- #to_s(omit: [], verbose: true) ⇒ Object
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_a ⇒ Object (readonly)
Returns the value of attribute to_a.
30 31 32 |
# File 'lib/subunit.rb', line 30 def to_a @to_a end |
#to_h ⇒ Object (readonly)
Returns the value of attribute to_h.
30 31 32 |
# File 'lib/subunit.rb', line 30 def to_h @to_h end |
#to_i ⇒ Object (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 %X e.g. 11 minutes 1 second %c e.g. 00:11:01
69 70 71 72 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 |
# File 'lib/subunit.rb', line 69 def strfunit(s) s.sub!('%x') do |x| to_h.map {|label, val| val.to_s + label[0]}.join(' ') end s.sub!('%X') do |x| to_h.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 end |
#to_s(omit: [], verbose: true) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/subunit.rb', line 99 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 |