subunit 0.8.0 → 0.8.5

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: 4466d231e3c336adf393301ac33736362e1ba2996bfa0a42f2ca56be949ffbff
4
- data.tar.gz: daff11e883761163e31f79d431f91ad1ad10790f613359a0aa593a29da82a57a
3
+ metadata.gz: 2e76018bbc5927b0645dc9b7b35df35041719bbcac77e078306edd8f00a83f04
4
+ data.tar.gz: 68057c6ef69c1cf21d554502f8c9055da642729614dadd3d2418f7fa8c55af51
5
5
  SHA512:
6
- metadata.gz: cd3f645fc57ea4badfbc121d02ba36edc240b063bebb468804dfe5a451b9d6abf084b3c59cc346302fad9e2fbda381aa82831f677789ac6c8bfd99b305dbf9fd
7
- data.tar.gz: 2fefff70fc6f6144c2f4fc5c0c691bd354ce42f770478ad8c8c93e8860175d833021900fc2b84ef6a363bd45eda21ef8dd4d4bb9b4fa83632425609d8f0e69db
6
+ metadata.gz: c322cef743c9ab764bd35158e68867cf5879e254427aad7351a2f3a0e222effc4a9597637f22f69250df7d058e4ad8e2ab42b74218a98db8e47d47da71433fe3
7
+ data.tar.gz: '09affcc2c6235d9bd29a03441b6bf64298a3aa7a46062b3a47753302a0f7d1088fec5d6c73c186e232181447dafeb6da1a1bb296303c109197aca89f9074a568'
checksums.yaml.gz.sig CHANGED
Binary file
data.tar.gz.sig CHANGED
Binary file
data/lib/subunit.rb CHANGED
@@ -16,10 +16,17 @@ end
16
16
 
17
17
  class Subunit
18
18
 
19
- def self.seconds(val)
20
- 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)
21
21
  end
22
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
+
23
30
  attr_reader :to_a, :to_h, :to_i
24
31
 
25
32
  def initialize(raw_units={}, obj)
@@ -27,19 +34,17 @@ class Subunit
27
34
  @debug = false
28
35
 
29
36
  @raw_units = raw_units
37
+
38
+ accumulate = ->(a) {
30
39
 
31
-
32
- accumulate = ->(obj) {
33
- a = obj[0..-2]
34
40
  len = raw_units.to_a.length
35
41
  val = ([0]*(len-1) + a).slice(-(len), len).zip(raw_units.values)\
36
- .inject(0) {|r,x| r + x.inject(:*) }
37
- val + obj[-1]
42
+ .inject(0) {|r,x| r * x[1] + x[0] }
38
43
  }
39
44
 
40
45
  if obj.is_a? String
41
46
 
42
- @to_i = accumulate.call obj.split(' ').map(&:to_i)
47
+ @to_i = accumulate.call obj.split(/\D+/).map(&:to_i)
43
48
 
44
49
  elsif obj.is_a? Array then
45
50
 
@@ -58,18 +63,34 @@ class Subunit
58
63
  # .strfunit("%x") #=> 11m 1s
59
64
  #
60
65
  # %x e.g. 11m 1s
66
+ # %xi e.g. 11m
61
67
  # %X e.g. 11 minutes 1 second
68
+ # %Xi e.g. 11 minutes
62
69
  # %c e.g. 00:11:01
70
+ # %s e.g. 11m # returns the 1st most significant value while
71
+ # ignoring the remainder
63
72
  #
64
73
  def strfunit(s)
65
74
 
66
- s.sub!('%x') do |x|
67
- 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
+ h = to_h
81
+ a = h.to_a
82
+ a2 = (a.length > 1 and x[-1][/i$/] and h[:seconds]) ? a[0..-2] : a
83
+ a2.map {|label, val| val.to_s + label[0]}.join(' ')
84
+
68
85
  end
69
86
 
70
- s.sub!('%X') do |x|
87
+ s.sub!(/%Xi?/) do |x|
88
+
89
+ h = to_h
90
+ a = h.to_a
91
+ a2 = (a.length > 1 and x[-1][/i$/] and h[:seconds]) ? a[0..-2] : a
71
92
 
72
- to_h.map do |label, val|
93
+ a2.map do |label, val|
73
94
 
74
95
  next if val == 0
75
96
  label2 = val > 1 ? label.to_s : label.to_s.sub(/s$/,'')
@@ -87,6 +108,11 @@ class Subunit
87
108
  fmt = a.map {|v| "%0" + v.to_s.length.to_s + "d"}.join(":")
88
109
  fmt % to_a
89
110
  end
111
+
112
+ s.sub!('%s') do |x|
113
+ label, val = to_h.first
114
+ val.to_s + label[0]
115
+ end
90
116
 
91
117
  s
92
118
  end
@@ -170,3 +196,4 @@ class Subunit
170
196
  unit_list.length > 0 ? [unit] + scan_units(unit_list, subunit) : [unit, subunit]
171
197
  end
172
198
  end
199
+
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.8.0
4
+ version: 0.8.5
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-10-08 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