Skip to content

Commit dedf775

Browse files
committed
Fix Style/OneClassPerFile offenses
Collapse the 14 reopenings of MultiJSONBenchmark in benchmark.rb into a single class body, fold the second module MultiJSON block in options_cache.rb into the first, and extract the deprecated MultiJson constant shim from lib/multi_json.rb into lib/multi_json/legacy_constant.rb, required after multi_json/deprecated so the forwarder snapshot still sees the complete public API.
1 parent eb943c8 commit dedf775

4 files changed

Lines changed: 64 additions & 88 deletions

File tree

benchmark.rb

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ def verify_preference(adapters, measurements)
4545
verifier.valid? ? 0 : 1
4646
end
4747
end
48-
end
4948

50-
class MultiJSONBenchmark
5149
# Command-line option parsing for the benchmark script.
5250
class CLI
5351
QUICK_OPTIONS = {
@@ -139,9 +137,7 @@ def validate_samples!(value)
139137
raise OptionParser::InvalidArgument, "--samples must be > 0" unless value.positive?
140138
end
141139
end
142-
end
143140

144-
class MultiJSONBenchmark
145141
# Resolves benchmarkable adapters from the current Ruby environment.
146142
class AdapterLoader
147143
def load(selected = nil)
@@ -162,9 +158,7 @@ def load_entry(name)
162158
nil
163159
end
164160
end
165-
end
166161

167-
class MultiJSONBenchmark
168162
# Builds the benchmark payload matrix across shapes and sizes.
169163
class PayloadCatalog
170164
CASES = [
@@ -206,9 +200,7 @@ def payload_case(shape, bucket, object)
206200
)
207201
end
208202
end
209-
end
210203

211-
class MultiJSONBenchmark
212204
# Builds flat scalar and array-heavy payloads.
213205
class ScalarPayloadFactory
214206
def flat_scalars(count)
@@ -256,9 +248,7 @@ def token(prefix, index, width)
256248
"#{prefix}_#{index.to_s(36).rjust(width, "0")}"
257249
end
258250
end
259-
end
260251

261-
class MultiJSONBenchmark
262252
# Builds nested object-heavy record payloads.
263253
class RecordPayloadFactory
264254
COUNTRIES = %w[US DE BR JP IN AU GB CA].freeze
@@ -341,9 +331,7 @@ def token(prefix, index, width)
341331
"#{prefix}_#{index.to_s(36).rjust(width, "0")}"
342332
end
343333
end
344-
end
345334

346-
class MultiJSONBenchmark
347335
# Runs the benchmark matrix across adapters, payloads, and operations.
348336
class Runner
349337
OPERATIONS = %i[parse generate].freeze
@@ -394,9 +382,7 @@ def prime_adapter!(adapter, payload)
394382
adapter.dump(payload.object)
395383
end
396384
end
397-
end
398385

399-
class MultiJSONBenchmark
400386
# Measures throughput for a single adapter/payload/operation combination.
401387
class Sampler
402388
def initialize(options)
@@ -462,9 +448,7 @@ def with_gc_disabled
462448
GC.enable unless already_disabled
463449
end
464450
end
465-
end
466451

467-
class MultiJSONBenchmark
468452
# Prints the benchmark summary and detailed result tables.
469453
class Reporter
470454
SUMMARY_HEADERS = ["adapter", "parse score", "generate score", "overall score", "wins"].freeze
@@ -513,9 +497,7 @@ def print_details
513497
)
514498
end
515499
end
516-
end
517500

518-
class MultiJSONBenchmark
519501
# Asserts MultiJSON::AdapterSelector::ADAPTERS matches the overall
520502
# benchmark throughput ranking.
521503
#
@@ -597,9 +579,7 @@ def tolerance_pct
597579
(TOLERANCE * 100).to_i
598580
end
599581
end
600-
end
601582

602-
class MultiJSONBenchmark
603583
# Computes overall adapter scores and benchmark wins.
604584
class Summary
605585
def initialize(adapters, measurements)
@@ -668,9 +648,7 @@ def normalized_ratio(value, peak)
668648
value / [peak, MultiJSONBenchmark::EPSILON].max
669649
end
670650
end
671-
end
672651

673-
class MultiJSONBenchmark
674652
# Builds the per-benchmark detail table rows.
675653
class Details
676654
def initialize(adapters, measurements)
@@ -734,9 +712,7 @@ def grouped_measurements
734712
@grouped_measurements ||= measurements.group_by { |measurement| [measurement.payload.label, measurement.operation] }
735713
end
736714
end
737-
end
738715

739-
class MultiJSONBenchmark
740716
# Renders plain-text and markdown tables for benchmark output.
741717
class TableRenderer
742718
def initialize(format:)
@@ -792,9 +768,7 @@ def align_cell(text, width, alignment)
792768
(alignment == :right) ? text.rjust(width) : text.ljust(width)
793769
end
794770
end
795-
end
796771

797-
class MultiJSONBenchmark
798772
# Shared numeric and display formatting helpers for benchmark output.
799773
class Formatter
800774
class << self

lib/multi_json.rb

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -267,54 +267,4 @@ def with_adapter(new_adapter, &)
267267
end
268268

269269
require_relative "multi_json/deprecated"
270-
271-
# Backward-compatible alias for the legacy ``MultiJson`` constant name
272-
#
273-
# Downstream code that still writes ``MultiJson.parse(...)`` or
274-
# ``rescue MultiJson::ParseError`` continues to work, but emits a
275-
# one-time deprecation warning pointing at ``MultiJSON``. Each public
276-
# method on {MultiJSON} gets an explicit forwarder defined on this
277-
# module, and constant access resolves via {.const_missing}, so both
278-
# dotted calls and ``::`` constant lookups (including rescue clauses)
279-
# route through the canonical module.
280-
#
281-
# @api public
282-
# @deprecated Use {MultiJSON} (all-caps) instead. Will be removed in v2.0.
283-
module MultiJson
284-
# Forward every public method MultiJSON exposes through an explicit
285-
# singleton method on the legacy MultiJson module, so callers that
286-
# capture the method as a Method object (``MultiJson.method(:load)``)
287-
# find this forwarder instead of falling back to inherited methods like
288-
# ``Kernel#load``. The earlier ``method_missing``-based shim left
289-
# ``MultiJson.method(:load)`` resolving to ``Kernel#load`` (because
290-
# ``Module#method`` doesn't consult ``method_missing``) and broke
291-
# libraries (Sawyer, Octokit, Danger) that capture decoders as Method
292-
# objects. Forwarding eagerly fixes the capture path while preserving
293-
# the one-time deprecation warning each call emits.
294-
(::MultiJSON.public_methods - ::Module.public_methods).each do |forwarded|
295-
define_singleton_method(forwarded) do |*args, **kwargs, &block|
296-
::MultiJSON.warn_deprecation_once(:multi_json_constant,
297-
"The MultiJson constant is deprecated and will be removed in v2.0. Use MultiJSON instead.")
298-
::MultiJSON.public_send(forwarded, *args, **kwargs, &block)
299-
end
300-
end
301-
302-
class << self
303-
# Resolve missing constants to their {MultiJSON} counterparts
304-
#
305-
# Enables ``rescue MultiJson::ParseError`` and
306-
# ``MultiJson::Adapters::Oj`` to keep working during the
307-
# deprecation cycle.
308-
#
309-
# @api public
310-
# @param name [Symbol] constant name
311-
# @return [Object] the resolved constant from {MultiJSON}
312-
# @example
313-
# MultiJson::ParseError # returns MultiJSON::ParseError
314-
def const_missing(name)
315-
::MultiJSON.warn_deprecation_once(:multi_json_constant,
316-
"The MultiJson constant is deprecated and will be removed in v2.0. Use MultiJSON instead.")
317-
::MultiJSON.const_get(name)
318-
end
319-
end
320-
end
270+
require_relative "multi_json/legacy_constant"

lib/multi_json/legacy_constant.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# frozen_string_literal: true
2+
3+
# Backward-compatible alias for the legacy ``MultiJson`` constant name
4+
#
5+
# Downstream code that still writes ``MultiJson.parse(...)`` or
6+
# ``rescue MultiJson::ParseError`` continues to work, but emits a
7+
# one-time deprecation warning pointing at ``MultiJSON``. Each public
8+
# method on {MultiJSON} gets an explicit forwarder defined on this
9+
# module, and constant access resolves via {.const_missing}, so both
10+
# dotted calls and ``::`` constant lookups (including rescue clauses)
11+
# route through the canonical module.
12+
#
13+
# This file is loaded last by the ``multi_json`` entry point so the
14+
# forwarder snapshot below sees the complete {MultiJSON} public API,
15+
# including the deprecated aliases defined in ``deprecated.rb``.
16+
#
17+
# @api public
18+
# @deprecated Use {MultiJSON} (all-caps) instead. Will be removed in v2.0.
19+
module MultiJson
20+
# Forward every public method MultiJSON exposes through an explicit
21+
# singleton method on the legacy MultiJson module, so callers that
22+
# capture the method as a Method object (``MultiJson.method(:load)``)
23+
# find this forwarder instead of falling back to inherited methods like
24+
# ``Kernel#load``. The earlier ``method_missing``-based shim left
25+
# ``MultiJson.method(:load)`` resolving to ``Kernel#load`` (because
26+
# ``Module#method`` doesn't consult ``method_missing``) and broke
27+
# libraries (Sawyer, Octokit, Danger) that capture decoders as Method
28+
# objects. Forwarding eagerly fixes the capture path while preserving
29+
# the one-time deprecation warning each call emits.
30+
(::MultiJSON.public_methods - ::Module.public_methods).each do |forwarded|
31+
define_singleton_method(forwarded) do |*args, **kwargs, &block|
32+
::MultiJSON.warn_deprecation_once(:multi_json_constant,
33+
"The MultiJson constant is deprecated and will be removed in v2.0. Use MultiJSON instead.")
34+
::MultiJSON.public_send(forwarded, *args, **kwargs, &block)
35+
end
36+
end
37+
38+
class << self
39+
# Resolve missing constants to their {MultiJSON} counterparts
40+
#
41+
# Enables ``rescue MultiJson::ParseError`` and
42+
# ``MultiJson::Adapters::Oj`` to keep working during the
43+
# deprecation cycle.
44+
#
45+
# @api public
46+
# @param name [Symbol] constant name
47+
# @return [Object] the resolved constant from {MultiJSON}
48+
# @example
49+
# MultiJson::ParseError # returns MultiJSON::ParseError
50+
def const_missing(name)
51+
::MultiJSON.warn_deprecation_once(:multi_json_constant,
52+
"The MultiJson constant is deprecated and will be removed in v2.0. Use MultiJSON instead.")
53+
::MultiJSON.const_get(name)
54+
end
55+
end
56+
end

lib/multi_json/options_cache.rb

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ module OptionsCache
1717
# via {.max_cache_size=}.
1818
DEFAULT_MAX_CACHE_SIZE = 1000
1919

20+
# Dynamic require path so MRI (mutex_store) and JRuby
21+
# (concurrent_store) execute the same physical line, avoiding a
22+
# dead-branch ``require_relative`` that would otherwise drop
23+
# JRuby's line coverage below 100%.
24+
BACKENDS = {"jruby" => "concurrent_store"}.freeze
25+
private_constant :BACKENDS
26+
2027
class << self
2128
# Get the dump options cache
2229
#
@@ -71,16 +78,5 @@ def reset
7178
end
7279
end
7380

74-
module MultiJSON
75-
module OptionsCache
76-
# Dynamic require path so MRI (mutex_store) and JRuby
77-
# (concurrent_store) execute the same physical line, avoiding a
78-
# dead-branch ``require_relative`` that would otherwise drop
79-
# JRuby's line coverage below 100%.
80-
BACKENDS = {"jruby" => "concurrent_store"}.freeze
81-
private_constant :BACKENDS
82-
end
83-
end
84-
8581
require_relative "options_cache/#{MultiJSON::OptionsCache.send(:const_get, :BACKENDS).fetch(RUBY_ENGINE, "mutex_store")}"
8682
MultiJSON::OptionsCache.reset

0 commit comments

Comments
 (0)