tago 0.3.0 → 0.5.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.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +15 -15
  3. data/Rakefile +1 -0
  4. data/lib/tago.rb +59 -8
  5. data/tago.gemspec +1 -1
  6. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e4a8085667f0c9d1b553d18f75411810fba9ab348490947c28a07b1cd0a95db0
4
- data.tar.gz: 3d67c0480ea190f4c46b0a9c7265a76ea1bec56ef5c11ccf2b4b4aad7fa999a7
3
+ metadata.gz: 7c58f911880a7e18c878c6d586393e785b4552205d5c9d489dcbf72e7aaab455
4
+ data.tar.gz: de65be56f9a1df5f45343644b50f27496c2daebdb423c4788b085803d8c17bd3
5
5
  SHA512:
6
- metadata.gz: f6bf77ea1cd83604f6b3a4103657f9b3c91aa063f68c90f6c1f4152f58e305fb19614b1f989b21311be8d1605e71ee111d9469635251c43eb484bfb4176a395e
7
- data.tar.gz: b67c0ac6171f76b0a3a18303bc61f5ec0cccdff7b51ee986c46fd0e5c3e7afa40e9ace61e31bdd3fa1fc0325f24c26f62dbc8ac98909c3daac28cc118ccd9247
6
+ metadata.gz: c797174c8dc2d002dffc12422f33e70d979fdc73c98150ea52e563cc0581f757444fca01fb492024736bc06c2edce67c6a0db136d57b410322770aae90dae7ff
7
+ data.tar.gz: cd6a4c966a36c85c7e1a8ac0f6cef233070b13e91c60375b543b44a431e55cb466d5d38b680a943a8426e62dae1599f0dd5f50824d847e0ff0237c5244f2d018
data/Gemfile.lock CHANGED
@@ -8,21 +8,21 @@ GEM
8
8
  specs:
9
9
  ast (2.4.3)
10
10
  docile (1.4.1)
11
- json (2.13.2)
11
+ json (2.15.2)
12
12
  language_server-protocol (3.17.0.5)
13
13
  lint_roller (1.1.0)
14
- minitest (5.25.5)
14
+ minitest (5.26.2)
15
15
  parallel (1.27.0)
16
- parser (3.3.9.0)
16
+ parser (3.3.10.0)
17
17
  ast (~> 2.4.1)
18
18
  racc
19
- prism (1.4.0)
19
+ prism (1.5.2)
20
20
  racc (1.8.1)
21
21
  rainbow (3.1.1)
22
- rake (13.3.0)
23
- regexp_parser (2.11.2)
22
+ rake (13.3.1)
23
+ regexp_parser (2.11.3)
24
24
  rexml (3.4.2)
25
- rubocop (1.80.2)
25
+ rubocop (1.81.7)
26
26
  json (~> 2.3)
27
27
  language_server-protocol (~> 3.17.0.2)
28
28
  lint_roller (~> 1.1.0)
@@ -30,20 +30,20 @@ GEM
30
30
  parser (>= 3.3.0.2)
31
31
  rainbow (>= 2.2.2, < 4.0)
32
32
  regexp_parser (>= 2.9.3, < 3.0)
33
- rubocop-ast (>= 1.46.0, < 2.0)
33
+ rubocop-ast (>= 1.47.1, < 2.0)
34
34
  ruby-progressbar (~> 1.7)
35
35
  unicode-display_width (>= 2.4.0, < 4.0)
36
- rubocop-ast (1.46.0)
36
+ rubocop-ast (1.47.1)
37
37
  parser (>= 3.3.7.2)
38
38
  prism (~> 1.4)
39
39
  rubocop-minitest (0.38.2)
40
40
  lint_roller (~> 1.1)
41
41
  rubocop (>= 1.75.0, < 2.0)
42
42
  rubocop-ast (>= 1.38.0, < 2.0)
43
- rubocop-performance (1.26.0)
43
+ rubocop-performance (1.26.1)
44
44
  lint_roller (~> 1.1)
45
45
  rubocop (>= 1.75.0, < 2.0)
46
- rubocop-ast (>= 1.44.0, < 2.0)
46
+ rubocop-ast (>= 1.47.1, < 2.0)
47
47
  rubocop-rake (0.7.1)
48
48
  lint_roller (~> 1.1)
49
49
  rubocop (>= 1.72.1)
@@ -57,10 +57,10 @@ GEM
57
57
  simplecov (~> 0.19)
58
58
  simplecov-html (0.13.2)
59
59
  simplecov_json_formatter (0.1.4)
60
- unicode-display_width (3.1.5)
61
- unicode-emoji (~> 4.0, >= 4.0.4)
62
- unicode-emoji (4.0.4)
63
- yard (0.9.37)
60
+ unicode-display_width (3.2.0)
61
+ unicode-emoji (~> 4.1)
62
+ unicode-emoji (4.1.0)
63
+ yard (0.9.38)
64
64
 
65
65
  PLATFORMS
66
66
  arm64-darwin-22
data/Rakefile CHANGED
@@ -31,6 +31,7 @@ require 'yard'
31
31
  desc 'Build Yard documentation'
32
32
  YARD::Rake::YardocTask.new do |t|
33
33
  t.files = ['lib/**/*.rb']
34
+ t.options = ['--fail-on-warning']
34
35
  end
35
36
 
36
37
  require 'rubocop/rake_task'
data/lib/tago.rb CHANGED
@@ -11,16 +11,61 @@ require 'time'
11
11
  # Copyright:: Copyright (c) 2024-2025 Yegor Bugayenko
12
12
  # License:: MIT
13
13
  class Float
14
- def seconds(format = nil)
14
+ def seconds(*args)
15
15
  s = self
16
16
  s = -s if s.negative?
17
+ if args.include?(:pretty)
18
+ locales = {
19
+ en: {
20
+ numbers: { 0 => 'zero', 1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five', 6 => 'six',
21
+ 7 => 'seven', 8 => 'eight', 9 => 'nine', 10 => 'ten' },
22
+ units: { microsecond: %w[microsecond microseconds], millisecond: %w[millisecond milliseconds],
23
+ second: %w[second seconds], minute: %w[minute minutes], hour: %w[hour hours], day: %w[day days],
24
+ week: %w[week weeks] },
25
+ short_units: {
26
+ microsecond: 'μs', millisecond: 'ms', second: 'sec', minute: 'min', hour: 'hr', day: 'd', week: 'wk'
27
+ }
28
+ }
29
+ }.freeze
30
+
31
+ if s < 0.001
32
+ val = (s * 1_000_000).to_i
33
+ unit = :microsecond
34
+ elsif s < 1
35
+ val = (s * 1000).to_i
36
+ unit = :millisecond
37
+ elsif s < 60
38
+ val = s.to_i
39
+ unit = :second
40
+ elsif s < 60 * 60
41
+ val = (s / 60).to_i
42
+ unit = :minute
43
+ elsif s < 24 * 60 * 60
44
+ val = (s / (60 * 60)).to_i
45
+ unit = :hour
46
+ elsif s < 7 * 24 * 60 * 60
47
+ val = (s / (24 * 60 * 60)).to_i
48
+ unit = :day
49
+ else
50
+ val = (s / (7 * 24 * 60 * 60)).to_i
51
+ unit = :week
52
+ end
53
+ short = args.include?(:short)
54
+ number_to_words = val <= 10 ? locales[:en][:numbers][val] : val.to_s
55
+ num = short ? val : number_to_words
56
+ names = locales[:en][:units][unit]
57
+ unit_symbol = val == 1 ? names[0] : names[1]
58
+ unit_name = short ? locales[:en][:short_units][unit] : unit_symbol
59
+ return format('%<num>s %<unit_name>s', num:, unit_name:)
60
+ end
61
+
17
62
  if s < 0.001
18
63
  format('%dμs', s * 1000 * 1000)
19
64
  elsif s < 1
20
65
  format('%dms', s * 1000)
21
66
  elsif s < 10
22
67
  ms = (s * 1000 % 1000).to_i
23
- if format == :round || ms.zero?
68
+ if args.include?(:round) || args.include?(:short) || ms.zero?
24
69
  format('%ds', s)
25
70
  else
26
71
  format('%<s>ds%<ms>dms', s:, ms:)
@@ -30,7 +75,7 @@ class Float
30
75
  elsif s < 60 * 60
31
76
  mins = (s / 60).to_i
32
77
  secs = (s % 60).to_i
33
- if format == :round || secs.zero?
78
+ if args.include?(:round) || args.include?(:short) || secs.zero?
34
79
  format('%dm', mins)
35
80
  else
36
81
  format('%<mins>dm%<secs>ds', mins:, secs:)
@@ -38,7 +83,7 @@ class Float
38
83
  elsif s < 24 * 60 * 60
39
84
  hours = (s / (60 * 60)).to_i
40
85
  mins = ((s % (60 * 60)) / 60).to_i
41
- if format == :round || mins.zero?
86
+ if args.include?(:round) || args.include?(:short) || mins.zero?
42
87
  format('%dh', hours)
43
88
  else
44
89
  format('%<hours>dh%<mins>dm', hours:, mins:)
@@ -46,7 +91,7 @@ class Float
46
91
  elsif s < 7 * 24 * 60 * 60
47
92
  days = (s / (24 * 60 * 60)).to_i
48
93
  hours = ((s % (24 * 60 * 60)) / (60 * 60)).to_i
49
- if format == :round || hours.zero?
94
+ if args.include?(:round) || args.include?(:short) || hours.zero?
50
95
  format('%dd', days)
51
96
  else
52
97
  format('%<days>dd%<hours>dh', days:, hours:)
@@ -54,7 +99,7 @@ class Float
54
99
  else
55
100
  weeks = (s / (7 * 24 * 60 * 60)).to_i
56
101
  days = (s / (24 * 60 * 60) % 7).to_i
57
- if format == :round || days.zero?
102
+ if args.include?(:round) || args.include?(:short) || days.zero?
58
103
  format('%dw', weeks)
59
104
  else
60
105
  format('%<weeks>dw%<days>dd', weeks:, days:)
@@ -69,7 +114,13 @@ end
69
114
  # Copyright:: Copyright (c) 2024-2025 Yegor Bugayenko
70
115
  # License:: MIT
71
116
  class Time
72
- def ago(now = Time.now)
73
- (now - self).seconds
117
+ def ago(arg = Time.now, *options)
118
+ if arg.is_a?(Time)
119
+ now = arg
120
+ else
121
+ now = Time.now
122
+ options = [arg] + options
123
+ end
124
+ (now - self).seconds(*options)
74
125
  end
75
126
  end
data/tago.gemspec CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
9
9
  s.required_rubygems_version = Gem::Requirement.new('>= 0') if s.respond_to? :required_rubygems_version=
10
10
  s.required_ruby_version = '>=3.0'
11
11
  s.name = 'tago'
12
- s.version = '0.3.0'
12
+ s.version = '0.5.0'
13
13
  s.license = 'MIT'
14
14
  s.summary = 'A new .ago() method for the Time class'
15
15
  s.description =
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tago
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko