constrain 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/constrain/version.rb +1 -1
- data/lib/constrain.rb +11 -8
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 48a0683db5a9b95baaf0ca8ee0f59322ffadbfe9f8b524a9e6e3d53cf7d1f06c
|
4
|
+
data.tar.gz: 3d07bc32462abdd359a85130301a29680f108dfe30f9af43ae1cda10f2a21d7d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fd9457aab6e64780ade38a804b914a3519a5c5f6cabf62cd35f94a5823eced188e2e9e89deb0980319ce6c6d565ebd97456f67b6a0657648226396b7476a0be2
|
7
|
+
data.tar.gz: 4f4fcedad8e23c7dcf7a1c52cc69c9f9361fbb95cd3e889c7295c6d0dbc85d67a547a0b9b9e4368d632a122ce1b6b3532a2ef89b00a92ea448a772360a47815a
|
data/lib/constrain/version.rb
CHANGED
data/lib/constrain.rb
CHANGED
@@ -3,8 +3,8 @@ require "constrain/version"
|
|
3
3
|
module Constrain
|
4
4
|
# Raised if types doesn't match a class expression
|
5
5
|
class MatchError < StandardError
|
6
|
-
def initialize(value, exprs,
|
7
|
-
super
|
6
|
+
def initialize(value, exprs, message: nil, unwind: 0)
|
7
|
+
super message || "Expected #{value.inspect} to match #{Constrain.fmt_exprs(exprs)}"
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
@@ -28,7 +28,7 @@ module Constrain
|
|
28
28
|
# constrain(value, *values, unwind: 0)
|
29
29
|
#
|
30
30
|
# Check that value matches one of the class expressions. Raises a
|
31
|
-
#
|
31
|
+
# ArgumentError if the expression is invalid and a Constrain::MatchError if
|
32
32
|
# the value doesn't match. The exception's backtrace skips :unwind number of
|
33
33
|
# entries
|
34
34
|
def self.constrain(value, *exprs)
|
@@ -36,14 +36,16 @@ module Constrain
|
|
36
36
|
end
|
37
37
|
|
38
38
|
# Return true if the value matches the class expression. Raises a
|
39
|
-
#
|
39
|
+
# ArgumentError if the expression is invalid
|
40
40
|
def self.constrain?(value, *exprs)
|
41
41
|
do_constrain?(value, *exprs)
|
42
42
|
end
|
43
43
|
|
44
44
|
module ClassMethods
|
45
45
|
# See Constrain.constrain
|
46
|
-
def constrain(*args)
|
46
|
+
def constrain(*args)
|
47
|
+
|
48
|
+
Constrain.do_constrain(*args) end
|
47
49
|
|
48
50
|
# See Constrain.constrain?
|
49
51
|
def constrain?(*args) Constrain.do_constrain?(*args) end
|
@@ -55,16 +57,17 @@ module Constrain
|
|
55
57
|
def self.do_constrain(value, *exprs)
|
56
58
|
if exprs.last.is_a?(Hash)
|
57
59
|
unwind = (exprs.last.delete(:unwind) || 0) + 1
|
58
|
-
|
60
|
+
message = exprs.last.delete(:message)
|
61
|
+
!exprs.last.empty? or exprs.pop # Remove option hash if empty
|
59
62
|
else
|
60
63
|
unwind = 1
|
64
|
+
message = nil
|
61
65
|
end
|
62
|
-
msg = exprs.pop if exprs.last.is_a?(String)
|
63
66
|
|
64
67
|
begin
|
65
68
|
!exprs.empty? or raise ArgumentError, "Empty constraint"
|
66
69
|
exprs.any? { |expr| Constrain.do_constrain_value?(value, expr) } or
|
67
|
-
raise MatchError.new(value, exprs,
|
70
|
+
raise MatchError.new(value, exprs, message: message, unwind: unwind)
|
68
71
|
rescue ArgumentError, Constrain::MatchError => ex
|
69
72
|
ex.set_backtrace(caller[1 + unwind..-1])
|
70
73
|
raise
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: constrain
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Claus Rasmussen
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-03-
|
11
|
+
date: 2022-03-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: simplecov
|