Skip to content

Commit bbe00ca

Browse files
committed
Rename ResultCountLimiters to Config, simplify code and make it THREAD SAFE
1 parent c7dc80a commit bbe00ca

File tree

3 files changed

+48
-43
lines changed

3 files changed

+48
-43
lines changed

lib/core_extensions/regexp/examples.rb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,13 @@ module Regexp
55
# No core classes are extended in any way, other than the above two methods.
66
module Examples
77
def examples(**config_options)
8-
RegexpExamples::ResultCountLimiters.with_configuration(
9-
max_repeater_variance: config_options[:max_repeater_variance],
10-
max_group_results: config_options[:max_group_results],
11-
max_results_limit: config_options[:max_results_limit]
12-
) do
8+
RegexpExamples::Config.with_configuration(config_options) do
139
examples_by_method(:result)
1410
end
1511
end
1612

1713
def random_example(**config_options)
18-
RegexpExamples::ResultCountLimiters.with_configuration(
19-
max_repeater_variance: config_options[:max_repeater_variance]
20-
) do
14+
RegexpExamples::Config.with_configuration(config_options) do
2115
examples_by_method(:random_result).sample
2216
end
2317
end

lib/regexp-examples/constants.rb

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,76 @@
11
# :nodoc:
22
module RegexpExamples
33
# Configuration settings to limit the number/length of Regexp examples generated
4-
class ResultCountLimiters
4+
class Config
55
class << self
6-
attr_accessor :max_repeater_variance, :max_group_results, :max_results_limit
7-
def with_configuration(max_repeater_variance: nil,
8-
max_group_results: nil,
9-
max_results_limit: nil)
10-
original_vals = [
11-
@max_repeater_variance,
12-
@max_group_results,
13-
@max_results_limit
14-
]
6+
def with_configuration(**new_config)
7+
original_config = config.dup
158

16-
@max_repeater_variance = max_repeater_variance if max_repeater_variance
17-
@max_group_results = max_group_results if max_group_results
18-
@max_results_limit = max_results_limit if max_results_limit
9+
begin
10+
self.config = new_config
11+
result = yield
12+
ensure
13+
self.config = original_config
14+
end
1915

20-
result = yield
16+
result
17+
end
2118

22-
@max_repeater_variance, @max_group_results, @max_results_limit = *original_vals
19+
# Thread-safe getters and setters
20+
%i[max_repeater_variance max_group_results max_results_limit].each do |m|
21+
define_method(m) do
22+
config[m]
23+
end
24+
define_method("#{m}=") do |value|
25+
config[m] = value
26+
end
27+
end
2328

24-
result
29+
private
30+
31+
def config=(**args)
32+
Thread.current[:regexp_examples_config].merge!(args)
33+
end
34+
35+
def config
36+
Thread.current[:regexp_examples_config] ||= {}
2537
end
2638
end
2739
# The maximum variance for any given repeater, to prevent a huge/infinite number of
28-
# examples from being listed. For example, if @@max_repeater_variance = 2 then:
40+
# examples from being listed. For example, if self.max_repeater_variance = 2 then:
2941
# .* is equivalent to .{0,2}
3042
# .+ is equivalent to .{1,3}
3143
# .{2,} is equivalent to .{2,4}
3244
# .{,3} is equivalent to .{0,2}
3345
# .{3,8} is equivalent to .{3,5}
3446
MAX_REPEATER_VARIANCE_DEFAULT = 2
35-
@max_repeater_variance = MAX_REPEATER_VARIANCE_DEFAULT
47+
self.max_repeater_variance = MAX_REPEATER_VARIANCE_DEFAULT
3648

3749
# Maximum number of characters returned from a char set, to reduce output spam
38-
# For example, if @@max_group_results = 5 then:
50+
# For example, if self.max_group_results = 5 then:
3951
# \d is equivalent to [01234]
4052
# \w is equivalent to [abcde]
4153
MAX_GROUP_RESULTS_DEFAULT = 5
42-
@max_group_results = MAX_GROUP_RESULTS_DEFAULT
54+
self.max_group_results = MAX_GROUP_RESULTS_DEFAULT
4355

4456
# Maximum number of results to be generated, for Regexp#examples
4557
# This is to prevent the system "freezing" when given instructions like:
4658
# /[ab]{30}/.examples
4759
# (Which would attempt to generate 2**30 == 1073741824 examples!!!)
4860
MAX_RESULTS_LIMIT_DEFAULT = 10_000
49-
@max_results_limit = MAX_RESULTS_LIMIT_DEFAULT
61+
self.max_results_limit = MAX_RESULTS_LIMIT_DEFAULT
5062
end
5163

5264
def self.max_repeater_variance
53-
ResultCountLimiters.max_repeater_variance
65+
Config.max_repeater_variance
5466
end
5567

5668
def self.max_group_results
57-
ResultCountLimiters.max_group_results
69+
Config.max_group_results
5870
end
5971

6072
def self.max_results_limit
61-
ResultCountLimiters.max_results_limit
73+
Config.max_results_limit
6274
end
6375

6476
# Definitions of various special characters, used in regular expressions.
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
RSpec.describe RegexpExamples::ResultCountLimiters do
1+
RSpec.describe RegexpExamples::Config do
22

33
describe 'max_repeater_variance' do
44
context 'as a passed parameter' do
@@ -38,11 +38,11 @@
3838

3939
context 'as a global setting' do
4040
before do
41-
@original = RegexpExamples::ResultCountLimiters.max_results_limit
42-
RegexpExamples::ResultCountLimiters.max_results_limit = 5
41+
@original = RegexpExamples::Config.max_results_limit
42+
RegexpExamples::Config.max_results_limit = 5
4343
end
4444
after do
45-
RegexpExamples::ResultCountLimiters.max_results_limit = @original
45+
RegexpExamples::Config.max_results_limit = @original
4646
end
4747

4848
it 'sets limit without passing explicitly' do
@@ -66,11 +66,11 @@
6666

6767
context 'as a global setting' do
6868
before do
69-
@original = RegexpExamples::ResultCountLimiters.max_repeater_variance
70-
RegexpExamples::ResultCountLimiters.max_repeater_variance = 5
69+
@original = RegexpExamples::Config.max_repeater_variance
70+
RegexpExamples::Config.max_repeater_variance = 5
7171
end
7272
after do
73-
RegexpExamples::ResultCountLimiters.max_repeater_variance = @original
73+
RegexpExamples::Config.max_repeater_variance = @original
7474
end
7575

7676
it 'sets limit without passing explicitly' do
@@ -94,11 +94,11 @@
9494

9595
context 'as a global setting' do
9696
before do
97-
@original = RegexpExamples::ResultCountLimiters.max_group_results
98-
RegexpExamples::ResultCountLimiters.max_group_results = 10
97+
@original = RegexpExamples::Config.max_group_results
98+
RegexpExamples::Config.max_group_results = 10
9999
end
100100
after do
101-
RegexpExamples::ResultCountLimiters.max_group_results = @original
101+
RegexpExamples::Config.max_group_results = @original
102102
end
103103

104104
it 'sets limit without passing explicitly' do
@@ -108,5 +108,4 @@
108108
end
109109
end # describe 'max_group_results'
110110

111-
112111
end

0 commit comments

Comments
 (0)