strict 1.4.0 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b15e13a3f628809ea9410898b170658979f5e4c5a69b03a1ef4dcccad35e947f
4
- data.tar.gz: b37173fddebaa55c744c20230548a80832a38c86e6bb4879f39d54a105fb5ddb
3
+ metadata.gz: 05af463032460cf8cf6cce2c976fe04a60315e8fcfba76984692ae5fe880dc23
4
+ data.tar.gz: 4c1668b6dc7282e128be060abd39f4b785a85857fa4648f14dbc2e22d745643c
5
5
  SHA512:
6
- metadata.gz: 25bb591ed6df44c60f6e2c01f977f4fcb881b022e62af11adbcd32e4d15dd83bfbfd34a1f20139aa64b0de0e8447e8cdc9e73d6ff5ac5331ea1ef9cab81f1f7e
7
- data.tar.gz: 46ddcaa771d227162e762d6bb424382c0efc3451d540340ad70a888842669ad8fb5969af11302d49ff5c5f5334852d44201be20253c399156373d9fd5d4c7ae8
6
+ metadata.gz: 442536f508b71231134f0bc291c12343d926737f53b8c8cb1ef575247ae4ac87435bba5a169bdda043daaa5e88c6ead88298520f824528903b1066d39dbf87a1
7
+ data.tar.gz: 81456be545bc010c0dac65fb3d2f9e390deb966c57b63032e81cf64a185a5268d0f14a40d6592c33db6793dc3bfb847548e76e6906ac7f9c4f8d6143a10f6151
data/Gemfile.lock CHANGED
@@ -1,51 +1,51 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- strict (1.4.0)
4
+ strict (1.5.0)
5
5
  zeitwerk (~> 2.6)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
10
  ast (2.4.2)
11
- debug (1.6.3)
12
- irb (>= 1.3.6)
11
+ debug (1.7.2)
12
+ irb (>= 1.5.0)
13
13
  reline (>= 0.3.1)
14
14
  gem-release (2.2.2)
15
- io-console (0.5.11)
16
- irb (1.4.2)
15
+ io-console (0.6.0)
16
+ irb (1.6.4)
17
17
  reline (>= 0.3.0)
18
- json (2.6.2)
19
- minitest (5.16.3)
18
+ json (2.6.3)
19
+ minitest (5.18.0)
20
20
  minitest-spec-context (0.0.4)
21
21
  parallel (1.22.1)
22
- parser (3.1.2.1)
22
+ parser (3.2.2.0)
23
23
  ast (~> 2.4.1)
24
24
  rainbow (3.1.1)
25
25
  rake (13.0.6)
26
- regexp_parser (2.6.0)
27
- reline (0.3.1)
26
+ regexp_parser (2.7.0)
27
+ reline (0.3.3)
28
28
  io-console (~> 0.5)
29
29
  rexml (3.2.5)
30
- rubocop (1.38.0)
30
+ rubocop (1.49.0)
31
31
  json (~> 2.3)
32
32
  parallel (~> 1.10)
33
- parser (>= 3.1.2.1)
33
+ parser (>= 3.2.0.0)
34
34
  rainbow (>= 2.2.2, < 4.0)
35
35
  regexp_parser (>= 1.8, < 3.0)
36
36
  rexml (>= 3.2.5, < 4.0)
37
- rubocop-ast (>= 1.23.0, < 2.0)
37
+ rubocop-ast (>= 1.28.0, < 2.0)
38
38
  ruby-progressbar (~> 1.7)
39
- unicode-display_width (>= 1.4.0, < 3.0)
40
- rubocop-ast (1.23.0)
41
- parser (>= 3.1.1.0)
42
- rubocop-minitest (0.23.0)
43
- rubocop (>= 0.90, < 2.0)
39
+ unicode-display_width (>= 2.4.0, < 3.0)
40
+ rubocop-ast (1.28.0)
41
+ parser (>= 3.2.1.0)
42
+ rubocop-minitest (0.30.0)
43
+ rubocop (>= 1.39, < 2.0)
44
44
  rubocop-rake (0.6.0)
45
45
  rubocop (~> 1.0)
46
- ruby-progressbar (1.11.0)
47
- unicode-display_width (2.3.0)
48
- zeitwerk (2.6.4)
46
+ ruby-progressbar (1.13.0)
47
+ unicode-display_width (2.4.2)
48
+ zeitwerk (2.6.7)
49
49
 
50
50
  PLATFORMS
51
51
  arm64-darwin-21
data/README.md CHANGED
@@ -173,6 +173,54 @@ storage = Storage.new(Storages::Wat.new)
173
173
  # => Strict::ImplementationDoesNotConformError
174
174
  ```
175
175
 
176
+ ### Configuration
177
+
178
+ Strict exposes some configuration options which can be configured globally via `Strict.configure { ... }` or overridden
179
+ within a block via `Strict.with_overrides(...) { ... }`.
180
+
181
+ #### Example
182
+
183
+ ```ruby
184
+ # Globally
185
+
186
+ Strict.configure do |c|
187
+ c.sample_rate = 0.75 # run validation ~75% of the time
188
+ end
189
+
190
+ Strict.configure do |c|
191
+ c.sample_rate = 0 # disable validation (Strict becomes Lenient)
192
+ end
193
+
194
+ Strict.configure do |c|
195
+ c.sample_rate = 0 # always run validation
196
+ end
197
+
198
+ # Locally within the block (only applies to the current thread)
199
+
200
+ Strict.with_overrides(sample_rate: 0) do
201
+ # Use Strict as you normally would
202
+
203
+ Strict.with_overrides(sample_rate: 0.5) do
204
+ # Overrides can be nested
205
+ end
206
+ end
207
+ ```
208
+
209
+ #### `Strict.configuration.random`
210
+
211
+ The instance of a `Random::Formatter` that Strict uses in tandom with the `sample_rate` to determine when validation
212
+ should be checked.
213
+
214
+ **Default**: `Random.new`
215
+
216
+ #### `Strict.configuration.sample_rate`
217
+
218
+ The rate of samples Strict will consider when validating attributes, parameters, and return values. A rate of 0.25 will
219
+ validate roughly 25% of the time, a rate of 0 will disable validation entirely, and a rate of 1 will always
220
+ run validations. The `sample_rate` is used in tandem with `random` to determine whether validation should be run.
221
+
222
+ **Default**: 1
223
+
176
224
  ## Development
177
225
 
178
226
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -57,6 +57,8 @@ module Strict
57
57
  end
58
58
 
59
59
  def valid?(value)
60
+ return true unless Strict.configuration.validate?
61
+
60
62
  validator === value
61
63
  end
62
64
 
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Strict
4
+ class Configuration
5
+ attr_reader :random, :sample_rate
6
+
7
+ def initialize(random: nil, sample_rate: nil)
8
+ self.random = random || Random.new
9
+ self.sample_rate = sample_rate || 1
10
+ end
11
+
12
+ def random=(random)
13
+ case random
14
+ when Random::Formatter
15
+ @random = random
16
+ else
17
+ raise Strict::Error, "Expected a Random::Formatter, got: #{random.inspect}."
18
+ end
19
+ end
20
+
21
+ def sample_rate=(rate)
22
+ case rate
23
+ when 0..1
24
+ @sample_rate = rate
25
+ else
26
+ raise Strict::Error, "Expected a sample rate between 0 and 1 (inclusive), got: #{rate.inspect}. " \
27
+ "A rate of 0 will disable strict validation. " \
28
+ "A rate of 1 will validate 100% of the time. " \
29
+ "A rate of 0.25 will validate roughly 25% of the time."
30
+ end
31
+ end
32
+
33
+ def validate?
34
+ sample_rate >= 1 || (sample_rate > 0 && random.rand < sample_rate) # rubocop:disable Style/NumericPredicate
35
+ end
36
+
37
+ def to_h
38
+ {
39
+ random: random,
40
+ sample_rate: sample_rate
41
+ }
42
+ end
43
+ end
44
+ end
@@ -56,6 +56,8 @@ module Strict
56
56
  end
57
57
 
58
58
  def valid?(value)
59
+ return true unless Strict.configuration.validate?
60
+
59
61
  validator === value
60
62
  end
61
63
 
data/lib/strict/return.rb CHANGED
@@ -16,6 +16,8 @@ module Strict
16
16
  end
17
17
 
18
18
  def valid?(value)
19
+ return true unless Strict.configuration.validate?
20
+
19
21
  validator === value
20
22
  end
21
23
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Strict
4
- VERSION = "1.4.0"
4
+ VERSION = "1.5.0"
5
5
  end
data/lib/strict.rb CHANGED
@@ -6,4 +6,45 @@ loader.setup
6
6
 
7
7
  module Strict
8
8
  ISSUE_TRACKER = "https://github.com/kylekthompson/strict/issues"
9
+
10
+ class << self
11
+ def configuration
12
+ thread_configuration || global_configuration
13
+ end
14
+
15
+ def configure
16
+ raise Strict::Error, "cannot reconfigure overridden configuration" if overridden?
17
+
18
+ yield(configuration)
19
+ end
20
+
21
+ def with_overrides(**overrides)
22
+ original_thread_configuration = thread_configuration
23
+
24
+ begin
25
+ self.thread_configuration = Strict::Configuration.new(**configuration.to_h.merge(overrides))
26
+ yield
27
+ ensure
28
+ self.thread_configuration = original_thread_configuration
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def overridden?
35
+ !!thread_configuration
36
+ end
37
+
38
+ def thread_configuration
39
+ Thread.current[:configuration]
40
+ end
41
+
42
+ def thread_configuration=(configuration)
43
+ Thread.current[:configuration] = configuration
44
+ end
45
+
46
+ def global_configuration
47
+ @global_configuration ||= Strict::Configuration.new
48
+ end
49
+ end
9
50
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: strict
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kyle Thompson
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-11-02 00:00:00.000000000 Z
11
+ date: 2023-04-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: zeitwerk
@@ -164,6 +164,7 @@ files:
164
164
  - lib/strict/attributes/instance.rb
165
165
  - lib/strict/coercers/array.rb
166
166
  - lib/strict/coercers/hash.rb
167
+ - lib/strict/configuration.rb
167
168
  - lib/strict/dsl/coercible.rb
168
169
  - lib/strict/dsl/validatable.rb
169
170
  - lib/strict/error.rb