|
1 | 1 | # :nodoc: |
2 | 2 | module RegexpExamples |
3 | 3 | # Configuration settings to limit the number/length of Regexp examples generated |
4 | | - class ResultCountLimiters |
| 4 | + class Config |
5 | 5 | 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 |
15 | 8 |
|
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 |
19 | 15 |
|
20 | | - result = yield |
| 16 | + result |
| 17 | + end |
21 | 18 |
|
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 |
23 | 28 |
|
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] ||= {} |
25 | 37 | end |
26 | 38 | end |
27 | 39 | # 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: |
29 | 41 | # .* is equivalent to .{0,2} |
30 | 42 | # .+ is equivalent to .{1,3} |
31 | 43 | # .{2,} is equivalent to .{2,4} |
32 | 44 | # .{,3} is equivalent to .{0,2} |
33 | 45 | # .{3,8} is equivalent to .{3,5} |
34 | 46 | MAX_REPEATER_VARIANCE_DEFAULT = 2 |
35 | | - @max_repeater_variance = MAX_REPEATER_VARIANCE_DEFAULT |
| 47 | + self.max_repeater_variance = MAX_REPEATER_VARIANCE_DEFAULT |
36 | 48 |
|
37 | 49 | # 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: |
39 | 51 | # \d is equivalent to [01234] |
40 | 52 | # \w is equivalent to [abcde] |
41 | 53 | MAX_GROUP_RESULTS_DEFAULT = 5 |
42 | | - @max_group_results = MAX_GROUP_RESULTS_DEFAULT |
| 54 | + self.max_group_results = MAX_GROUP_RESULTS_DEFAULT |
43 | 55 |
|
44 | 56 | # Maximum number of results to be generated, for Regexp#examples |
45 | 57 | # This is to prevent the system "freezing" when given instructions like: |
46 | 58 | # /[ab]{30}/.examples |
47 | 59 | # (Which would attempt to generate 2**30 == 1073741824 examples!!!) |
48 | 60 | MAX_RESULTS_LIMIT_DEFAULT = 10_000 |
49 | | - @max_results_limit = MAX_RESULTS_LIMIT_DEFAULT |
| 61 | + self.max_results_limit = MAX_RESULTS_LIMIT_DEFAULT |
50 | 62 | end |
51 | 63 |
|
52 | 64 | def self.max_repeater_variance |
53 | | - ResultCountLimiters.max_repeater_variance |
| 65 | + Config.max_repeater_variance |
54 | 66 | end |
55 | 67 |
|
56 | 68 | def self.max_group_results |
57 | | - ResultCountLimiters.max_group_results |
| 69 | + Config.max_group_results |
58 | 70 | end |
59 | 71 |
|
60 | 72 | def self.max_results_limit |
61 | | - ResultCountLimiters.max_results_limit |
| 73 | + Config.max_results_limit |
62 | 74 | end |
63 | 75 |
|
64 | 76 | # Definitions of various special characters, used in regular expressions. |
|
0 commit comments