subunit 0.6.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 91907a5881a1b4f38c855693b78cd72c7ab944edc8370b78922ac261bfdf8165
4
- data.tar.gz: b0ecdcfe9761bceb2abfab49fd353e56cc6809590a4047cc26af262bf38ba0d7
3
+ metadata.gz: 4466d231e3c336adf393301ac33736362e1ba2996bfa0a42f2ca56be949ffbff
4
+ data.tar.gz: daff11e883761163e31f79d431f91ad1ad10790f613359a0aa593a29da82a57a
5
5
  SHA512:
6
- metadata.gz: 4a653b499f5d8521dc18c488c804f5506a2120ce2255e2b5cd027dc64ac7b4104f719e9e12bb4d9109c976b3990995ee6a1ed39183f488b5df7d00905e890a68
7
- data.tar.gz: 6f4b1109dce2045afdd768478feac71a9990a098e2be27669e4ee3282e1f850e6e3d5d8f5ac3bffbc477867f552ebd61fe14e4b24aa79a5e509cb4c096553ba5
6
+ metadata.gz: cd3f645fc57ea4badfbc121d02ba36edc240b063bebb468804dfe5a451b9d6abf084b3c59cc346302fad9e2fbda381aa82831f677789ac6c8bfd99b305dbf9fd
7
+ data.tar.gz: 2fefff70fc6f6144c2f4fc5c0c691bd354ce42f770478ad8c8c93e8860175d833021900fc2b84ef6a363bd45eda21ef8dd4d4bb9b4fa83632425609d8f0e69db
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -2,7 +2,23 @@
2
2
 
3
3
  # file: subunit.rb
4
4
 
5
+ module SubunitFracture
6
+
7
+ refine Integer do
8
+
9
+ def strfunit(s)
10
+ Subunit.seconds(self).strfunit(s)
11
+ end
12
+
13
+ end
14
+
15
+ end
16
+
5
17
  class Subunit
18
+
19
+ def self.seconds(val)
20
+ new(units={minutes:60, hours:60}, seconds: val)
21
+ end
6
22
 
7
23
  attr_reader :to_a, :to_h, :to_i
8
24
 
@@ -10,13 +26,24 @@ class Subunit
10
26
 
11
27
  @debug = false
12
28
 
13
- if obj.is_a? Array then
14
-
29
+ @raw_units = raw_units
30
+
31
+
32
+ accumulate = ->(obj) {
15
33
  a = obj[0..-2]
16
34
  len = raw_units.to_a.length
17
35
  val = ([0]*(len-1) + a).slice(-(len), len).zip(raw_units.values)\
18
36
  .inject(0) {|r,x| r + x.inject(:*) }
19
- @to_i = val + obj[-1]
37
+ val + obj[-1]
38
+ }
39
+
40
+ if obj.is_a? String
41
+
42
+ @to_i = accumulate.call obj.split(' ').map(&:to_i)
43
+
44
+ elsif obj.is_a? Array then
45
+
46
+ @to_i = accumulate.call obj
20
47
 
21
48
  else
22
49
 
@@ -30,11 +57,38 @@ class Subunit
30
57
  # usage: Subunit.new(units={minutes:60, hours:60}, seconds: 661)\
31
58
  # .strfunit("%x") #=> 11m 1s
32
59
  #
33
- def strfunit(s)
60
+ # %x e.g. 11m 1s
61
+ # %X e.g. 11 minutes 1 second
62
+ # %c e.g. 00:11:01
63
+ #
64
+ def strfunit(s)
34
65
 
35
- s2 = to_h.map {|label, val| val.to_s + label[0]}.join(' ')
36
- s.sub('%x', s2)
66
+ s.sub!('%x') do |x|
67
+ to_h.map {|label, val| val.to_s + label[0]}.join(' ')
68
+ end
69
+
70
+ s.sub!('%X') do |x|
71
+
72
+ to_h.map do |label, val|
73
+
74
+ next if val == 0
75
+ label2 = val > 1 ? label.to_s : label.to_s.sub(/s$/,'')
76
+ val.to_s + ' ' + label2
77
+
78
+ end.compact.join(' ')
79
+
80
+ end
81
+
82
+ s.sub!('%c') do |x|
83
+
84
+ a = @raw_units.values.reverse
85
+ a << a[-1]
86
+
87
+ fmt = a.map {|v| "%0" + v.to_s.length.to_s + "d"}.join(":")
88
+ fmt % to_a
89
+ end
37
90
 
91
+ s
38
92
  end
39
93
 
40
94
  def to_s(omit: [], verbose: true)
@@ -116,4 +170,3 @@ class Subunit
116
170
  unit_list.length > 0 ? [unit] + scan_units(unit_list, subunit) : [unit, subunit]
117
171
  end
118
172
  end
119
-
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: subunit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -35,7 +35,7 @@ cert_chain:
35
35
  HowcHg4GymtLLITCF/QtA5tta5SmHAca1POGncsO9QiramFQ9S3Q4O4wcjboVJOp
36
36
  SSxk4InmYoY9Cl2GVQ+d9owo
37
37
  -----END CERTIFICATE-----
38
- date: 2020-09-05 00:00:00.000000000 Z
38
+ date: 2020-10-08 00:00:00.000000000 Z
39
39
  dependencies: []
40
40
  description:
41
41
  email: james@jamesrobertson.eu
metadata.gz.sig CHANGED
Binary file