subunit 0.7.0 → 0.8.4

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: ecc45950417e19ac0a19525e892afae96c9bdfd000e13ecd5638e19cf210a564
4
- data.tar.gz: 8332af06d795048f2736f31a11c5ffecf59dbe7a8005a2ed7751e9b5b4037725
3
+ metadata.gz: 55e24efd3496c6090f8b887857fbefd3dc2a76f4a61fb4a8c3a6f19490bfa153
4
+ data.tar.gz: fd0af0f6e4fd910898141aa6c7bc13049cd2cd728ba4a9e19a5ab5a8cf9bf558
5
5
  SHA512:
6
- metadata.gz: 407ad2ccc1947d1adc2f32c98a606bda8d453be2bc18217b48a09ee9ac6010568db36e54577069188ad4ac319f17cf336ca60d691e524efd55f2c4e8eff92b72
7
- data.tar.gz: 3360ae777ff80f1f7f81735be7321dea7bc7542c8903467c6427cd4dcbaf168dfc8c01453100fe55db6e385ff7a8717f2f0cf2f3073aaf562e4d148376b83dfb
6
+ metadata.gz: 9c2ff98f077a42af3dffc3788961f99bd98fb6643c5987f472e640094a485c138a8a4065a0832c98f27fd4b578d3e0972f1c5ae44a019014471e5cb2ef8e5bc1
7
+ data.tar.gz: 9ea37778dd9fe8cf32278572a70d4d188ee9b59a460e5e8139bc2e2ff28a13a8055d71d79a7205738c2504fdf595e76ab29c5825f7b749db392a30dedd8a784b
checksums.yaml.gz.sig CHANGED
Binary file
data.tar.gz.sig CHANGED
Binary file
data/lib/subunit.rb CHANGED
@@ -2,12 +2,31 @@
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
6
18
 
7
- def self.seconds(val)
8
- new(units={minutes:60, hours:60}, seconds: val)
19
+ def self.seconds(val, units: nil)
20
+ new(units={minutes:60, hours:60, days:24}, seconds: val)
9
21
  end
10
22
 
23
+ # e.g. Subunit.hms_to_seconds('5 minutes and 4 seconds')
24
+ # => 304
25
+ #
26
+ def self.hms_to_seconds(obj)
27
+ new(units={hours:60, minutes:60,seconds: 60, }, obj).to_i
28
+ end
29
+
11
30
  attr_reader :to_a, :to_h, :to_i
12
31
 
13
32
  def initialize(raw_units={}, obj)
@@ -15,14 +34,21 @@ class Subunit
15
34
  @debug = false
16
35
 
17
36
  @raw_units = raw_units
37
+
38
+ accumulate = ->(a) {
18
39
 
19
- if obj.is_a? Array then
20
-
21
- a = obj[0..-2]
22
40
  len = raw_units.to_a.length
23
41
  val = ([0]*(len-1) + a).slice(-(len), len).zip(raw_units.values)\
24
- .inject(0) {|r,x| r + x.inject(:*) }
25
- @to_i = val + obj[-1]
42
+ .inject(0) {|r,x| r * x[1] + x[0] }
43
+ }
44
+
45
+ if obj.is_a? String
46
+
47
+ @to_i = accumulate.call obj.split(/\D+/).map(&:to_i)
48
+
49
+ elsif obj.is_a? Array then
50
+
51
+ @to_i = accumulate.call obj
26
52
 
27
53
  else
28
54
 
@@ -37,18 +63,32 @@ class Subunit
37
63
  # .strfunit("%x") #=> 11m 1s
38
64
  #
39
65
  # %x e.g. 11m 1s
66
+ # %xi e.g. 11m
40
67
  # %X e.g. 11 minutes 1 second
68
+ # %Xi e.g. 11 minutes
41
69
  # %c e.g. 00:11:01
70
+ # %s e.g. 11m # returns the 1st most significant value while
71
+ # ignoring the remainder
42
72
  #
43
73
  def strfunit(s)
44
74
 
45
- s.sub!('%x') do |x|
46
- to_h.map {|label, val| val.to_s + label[0]}.join(' ')
75
+ # The i switch postfixed to the x or X switch will format the output
76
+ # without seconds unless there are no minutes.
77
+
78
+ s.sub!(/%xi?/) do |x|
79
+
80
+ a = to_h.to_a
81
+ a2 = (a.length > 1 and x[-1][/i$/]) ? a[0..-2] : a
82
+ a2.map {|label, val| val.to_s + label[0]}.join(' ')
83
+
47
84
  end
48
85
 
49
- s.sub!('%X') do |x|
86
+ s.sub!(/%Xi?/) do |x|
87
+
88
+ a = to_h.to_a
89
+ a2 = (a.length > 1 and x[-1][/i$/]) ? a[0..-2] : a
50
90
 
51
- to_h.map do |label, val|
91
+ a2.map do |label, val|
52
92
 
53
93
  next if val == 0
54
94
  label2 = val > 1 ? label.to_s : label.to_s.sub(/s$/,'')
@@ -66,6 +106,11 @@ class Subunit
66
106
  fmt = a.map {|v| "%0" + v.to_s.length.to_s + "d"}.join(":")
67
107
  fmt % to_a
68
108
  end
109
+
110
+ s.sub!('%s') do |x|
111
+ label, val = to_h.first
112
+ val.to_s + label[0]
113
+ end
69
114
 
70
115
  s
71
116
  end
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.7.0
4
+ version: 0.8.4
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-14 00:00:00.000000000 Z
38
+ date: 2021-04-07 00:00:00.000000000 Z
39
39
  dependencies: []
40
40
  description:
41
41
  email: james@jamesrobertson.eu
@@ -63,7 +63,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
63
63
  - !ruby/object:Gem::Version
64
64
  version: '0'
65
65
  requirements: []
66
- rubygems_version: 3.0.3
66
+ rubyforge_project:
67
+ rubygems_version: 2.7.10
67
68
  signing_key:
68
69
  specification_version: 4
69
70
  summary: Input a raw unit and it returns the denomination.
metadata.gz.sig CHANGED
Binary file