Skip to content

Commit d38f861

Browse files
committed
Use String::Format gem for formatting.
1 parent fcaf66c commit d38f861

File tree

4 files changed

+20
-42
lines changed

4 files changed

+20
-42
lines changed

async-service.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ Gem::Specification.new do |spec|
2828

2929
spec.add_dependency "async"
3030
spec.add_dependency "async-container", "~> 0.16"
31+
spec.add_dependency "string-format", "~> 0.2"
3132
end

lib/async/service/formatting.rb

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,24 @@
33
# Released under the MIT License.
44
# Copyright, 2025, by Samuel Williams.
55

6+
require "string/format"
7+
68
module Async
79
module Service
810
# Formatting utilities for service titles.
911
#
1012
# Services need meaningful process/thread names for monitoring and debugging. This module provides consistent formatting for common service metrics like connection counts, request ratios, and load values in process titles.
1113
#
1214
# It is expected you will include these into your service class and use them to update the `instance.name` in the health check.
15+
#
16+
# @deprecated Use {String::Format} directly.
1317
module Formatting
14-
UNITS = [nil, "K", "M", "B", "T", "P", "E", "Z", "Y"]
15-
1618
# Format a count into a human-readable string.
1719
# @parameter value [Numeric] The count to format.
18-
# @parameter units [Array] The units to use for formatting (default: UNITS).
20+
# @parameter units [Array] The units to use for formatting (default: String::Format::UNITS).
1921
# @returns [String] A formatted string representing the count.
20-
def format_count(value, units = UNITS)
21-
value = value
22-
index = 0
23-
limit = units.size - 1
24-
25-
# Handle negative numbers by working with absolute value:
26-
negative = value < 0
27-
value = value.abs
28-
29-
while value >= 1000 and index < limit
30-
value = value / 1000.0
31-
index += 1
32-
end
33-
34-
result = String.new
35-
result << "-" if negative
36-
result << value.round(2).to_s
37-
result << units[index].to_s if units[index]
38-
39-
return result
22+
def format_count(value, units = String::Format::UNITS)
23+
String::Format.count(value, units)
4024
end
4125

4226
module_function :format_count
@@ -46,7 +30,7 @@ def format_count(value, units = UNITS)
4630
# @parameter total [Numeric] The total value.
4731
# @returns [String] A formatted ratio string.
4832
def format_ratio(current, total)
49-
"#{format_count(current)}/#{format_count(total)}"
33+
String::Format.ratio(current, total)
5034
end
5135

5236
module_function :format_ratio
@@ -55,27 +39,16 @@ def format_ratio(current, total)
5539
# @parameter load [Numeric] The load value (typically 0.0 to 1.0+).
5640
# @returns [String] A formatted load string.
5741
def format_load(load)
58-
load.round(2).to_s
42+
String::Format.decimal(load)
5943
end
6044

6145
module_function :format_load
6246

6347
# Format multiple statistics into a compact string.
64-
# @parameter stats [Hash] Hash of statistic names to values or [current, total] arrays.
48+
# @parameter pairs [Hash] Hash of statistic names to values or [current, total] arrays.
6549
# @returns [String] A formatted statistics string.
6650
def format_statistics(**pairs)
67-
pairs.map do |key, value|
68-
case value
69-
when Array
70-
if value.length == 2
71-
"#{key.to_s.upcase}=#{format_ratio(value[0], value[1])}"
72-
else
73-
"#{key.to_s.upcase}=#{value.join('/')}"
74-
end
75-
else
76-
"#{key.to_s.upcase}=#{format_count(value)}"
77-
end
78-
end.join(" ")
51+
String::Format.statistics(pairs)
7952
end
8053

8154
module_function :format_statistics

releases.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Releases
22

3+
## Unreleased
4+
5+
- Use `String::Format` gem for formatting.
6+
37
## v0.14.0
48

59
- Introduce `ContainerEnvironment` and `ContainerService` for implementing best-practice services.

test/async/service/formatting.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117
with "#format_statistics" do
118118
it "formats single values" do
119119
result = subject.format_statistics(connections: 23)
120-
expect(result).to be == "CONNECTIONS=23"
120+
expect(result).to be == "C=23"
121121
end
122122

123123
it "formats ratios from arrays" do
@@ -139,12 +139,12 @@
139139
active: 5,
140140
load: 0.273
141141
)
142-
expect(result).to be == "CONNECTIONS=23/3.42K ACTIVE=5 LOAD=0.27"
142+
expect(result).to be == "C=23/3.42K A=5 L=0.27"
143143
end
144144

145145
it "handles arrays with more than 2 elements" do
146146
result = subject.format_statistics(multi: [1, 2, 3])
147-
expect(result).to be == "MULTI=1/2/3"
147+
expect(result).to be == "M=1/2/3"
148148
end
149149

150150
it "handles empty statistics" do
@@ -157,7 +157,7 @@
157157
:symbol_key => 100,
158158
"string_key" => 200
159159
)
160-
expect(result).to be == "SYMBOL_KEY=100 STRING_KEY=200"
160+
expect(result).to be == "S=100 S=200"
161161
end
162162
end
163163

0 commit comments

Comments
 (0)