tago 0.4.0 → 0.6.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 (7) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +5 -5
  3. data/README.md +21 -0
  4. data/Rakefile +1 -0
  5. data/lib/tago.rb +25 -21
  6. data/tago.gemspec +1 -1
  7. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6ce49c7e11433395a7963fd3b8ae9a7bfc10dbb00e10fb0a2791b13fb432cf35
4
- data.tar.gz: 861df150e33bcdc48e0d8ec3a34c2a33a5c455be7b19b7b6ebad11e166772b1c
3
+ metadata.gz: 7065628d4d10a5ecb484a5a08973d4091bdcb94858932b7ff353e1472d4572d0
4
+ data.tar.gz: 4f41a9476eb8838e0476e4586e226a81fd7b4874518cc9ab5c856df79e7b56e1
5
5
  SHA512:
6
- metadata.gz: afdfb3de82966a409d8d59498788eaf623d5251f1bd0561e5025662d12a28d705ac96e236014c539fe1e0e4c6270367687d4000f540cd46a5afd1d73c09ddf18
7
- data.tar.gz: ddd745356b376ec6254cf5bfec47b5a388c5d3e595d30ed99d7729bcb91ff678e03a6b3722a891fb569582937b9b0d0078931bde34861efb83dc1f9c5829c2e8
6
+ metadata.gz: b719d14e52e638a19e74d1f25d1c17d5b8e435d88d611fbb91290add6646374639300eb091c711e6e34f000294d28937f12bc3c9e89d70a9e014c7fc763bd775
7
+ data.tar.gz: 6f7d4e72147f350ec57d4bcb9193ec1976e6e8aea47f2e411482b6cbe3dd4acf65fda17e9a7f8bce410b12efd7c4646b69d6c568a9b7e566e9b5d8f05b9ca42e
data/Gemfile.lock CHANGED
@@ -8,12 +8,12 @@ GEM
8
8
  specs:
9
9
  ast (2.4.3)
10
10
  docile (1.4.1)
11
- json (2.15.1)
11
+ json (2.15.2)
12
12
  language_server-protocol (3.17.0.5)
13
13
  lint_roller (1.1.0)
14
- minitest (5.26.0)
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
19
  prism (1.5.2)
@@ -22,7 +22,7 @@ GEM
22
22
  rake (13.3.1)
23
23
  regexp_parser (2.11.3)
24
24
  rexml (3.4.2)
25
- rubocop (1.81.6)
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)
@@ -60,7 +60,7 @@ GEM
60
60
  unicode-display_width (3.2.0)
61
61
  unicode-emoji (~> 4.1)
62
62
  unicode-emoji (4.1.0)
63
- yard (0.9.37)
63
+ yard (0.9.38)
64
64
 
65
65
  PLATFORMS
66
66
  arm64-darwin-22
data/README.md CHANGED
@@ -32,6 +32,27 @@ s = Time.now - start
32
32
  puts "It took #{s.seconds}"
33
33
  ```
34
34
 
35
+ The `seconds` method accepts optional formatting flags:
36
+
37
+ ```ruby
38
+ 9.444.seconds # => "9s444ms"
39
+ 9.444.seconds(:round) # => "9s" (omits sub-units)
40
+ 9.444.seconds(:short) # => "9s" (also omits sub-units)
41
+ 300.0.seconds(:pretty) # => "five minutes" (words instead of abbreviations)
42
+ 300.0.seconds(:pretty, :short) # => "5 min" (numeric with short unit names)
43
+ 5.0.seconds(:pretty, :caps) # => "Five seconds" (capitalizes first letter)
44
+ ```
45
+
46
+ The same flags work with `ago`:
47
+
48
+ ```ruby
49
+ start.ago(:round)
50
+ start.ago(:pretty)
51
+ start.ago(:pretty, :short)
52
+ start.ago(:pretty, :caps)
53
+ start.ago(Time.now, :round)
54
+ ```
55
+
35
56
  The gem basically extends the `Float` and `Time` classes with new methods.
36
57
 
37
58
  ## How to contribute
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,17 +11,20 @@ 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 format == :pretty
17
+ if args.include?(:pretty)
18
18
  locales = {
19
19
  en: {
20
20
  numbers: { 0 => 'zero', 1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five', 6 => 'six',
21
21
  7 => 'seven', 8 => 'eight', 9 => 'nine', 10 => 'ten' },
22
22
  units: { microsecond: %w[microsecond microseconds], millisecond: %w[millisecond milliseconds],
23
23
  second: %w[second seconds], minute: %w[minute minutes], hour: %w[hour hours], day: %w[day days],
24
- week: %w[week weeks] }
24
+ week: %w[week weeks] },
25
+ short_units: {
26
+ microsecond: 'μs', millisecond: 'ms', second: 'sec', minute: 'min', hour: 'hr', day: 'd', week: 'wk'
27
+ }
25
28
  }
26
29
  }.freeze
27
30
 
@@ -47,11 +50,14 @@ class Float
47
50
  val = (s / (7 * 24 * 60 * 60)).to_i
48
51
  unit = :week
49
52
  end
50
-
51
- num = val < 10 ? locales[:en][:numbers][val] : val.to_s
52
- names = locales[:en][:units].fetch(unit)
53
- unit_name = val == 1 ? names[0] : names[1]
54
- return format('%<num>s %<unit_name>s', num:, unit_name:)
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
+ result = format('%<num>s %<unit_name>s', num:, unit_name:)
60
+ return args.include?(:caps) ? result.capitalize : result
55
61
  end
56
62
 
57
63
  if s < 0.001
@@ -60,7 +66,7 @@ class Float
60
66
  format('%dms', s * 1000)
61
67
  elsif s < 10
62
68
  ms = (s * 1000 % 1000).to_i
63
- if format == :round || ms.zero?
69
+ if args.include?(:round) || args.include?(:short) || ms.zero?
64
70
  format('%ds', s)
65
71
  else
66
72
  format('%<s>ds%<ms>dms', s:, ms:)
@@ -70,7 +76,7 @@ class Float
70
76
  elsif s < 60 * 60
71
77
  mins = (s / 60).to_i
72
78
  secs = (s % 60).to_i
73
- if format == :round || secs.zero?
79
+ if args.include?(:round) || args.include?(:short) || secs.zero?
74
80
  format('%dm', mins)
75
81
  else
76
82
  format('%<mins>dm%<secs>ds', mins:, secs:)
@@ -78,7 +84,7 @@ class Float
78
84
  elsif s < 24 * 60 * 60
79
85
  hours = (s / (60 * 60)).to_i
80
86
  mins = ((s % (60 * 60)) / 60).to_i
81
- if format == :round || mins.zero?
87
+ if args.include?(:round) || args.include?(:short) || mins.zero?
82
88
  format('%dh', hours)
83
89
  else
84
90
  format('%<hours>dh%<mins>dm', hours:, mins:)
@@ -86,7 +92,7 @@ class Float
86
92
  elsif s < 7 * 24 * 60 * 60
87
93
  days = (s / (24 * 60 * 60)).to_i
88
94
  hours = ((s % (24 * 60 * 60)) / (60 * 60)).to_i
89
- if format == :round || hours.zero?
95
+ if args.include?(:round) || args.include?(:short) || hours.zero?
90
96
  format('%dd', days)
91
97
  else
92
98
  format('%<days>dd%<hours>dh', days:, hours:)
@@ -94,7 +100,7 @@ class Float
94
100
  else
95
101
  weeks = (s / (7 * 24 * 60 * 60)).to_i
96
102
  days = (s / (24 * 60 * 60) % 7).to_i
97
- if format == :round || days.zero?
103
+ if args.include?(:round) || args.include?(:short) || days.zero?
98
104
  format('%dw', weeks)
99
105
  else
100
106
  format('%<weeks>dw%<days>dd', weeks:, days:)
@@ -109,15 +115,13 @@ end
109
115
  # Copyright:: Copyright (c) 2024-2025 Yegor Bugayenko
110
116
  # License:: MIT
111
117
  class Time
112
- def ago(arg = Time.now, pretty = nil)
113
- if arg.is_a?(Symbol) || arg.nil?
114
- format = arg
115
- now = pretty || Time.now
116
- else
118
+ def ago(arg = Time.now, *options)
119
+ if arg.is_a?(Time)
117
120
  now = arg
118
- format = pretty
121
+ else
122
+ now = Time.now
123
+ options = [arg] + options
119
124
  end
120
-
121
- (now - self).seconds(format)
125
+ (now - self).seconds(*options)
122
126
  end
123
127
  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.4.0'
12
+ s.version = '0.6.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.4.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko