constrain 0.3.3 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/constrain/version.rb +1 -1
- data/lib/constrain.rb +7 -11
- 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: c76b91e6d73a1c5d2f7c44fc98f372f5b911c08e6a6d4203a3be646019aaa1a3
|
4
|
+
data.tar.gz: da85fb71b85da4e54913cb227a162c901ef3fe88c61d091e3489a63a3a97a9e5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3db8dbbb8a0b2eb8d0c8de705b85533a630748f9e02c4e82f324c8f705d9f01d251b4d9b9866d977b71aaef4beb29f1834f1ff058728055ef545d77ece25a506
|
7
|
+
data.tar.gz: 52a2ec74e4709506d7be7813b48d5a7ece977dc7314d591db5830033f2e477cab39d2dc26129e7d0d27727f57966b0c0957faf5ba701ef9f6754ca16123f342a
|
data/lib/constrain/version.rb
CHANGED
data/lib/constrain.rb
CHANGED
@@ -1,11 +1,8 @@
|
|
1
1
|
require "constrain/version"
|
2
2
|
|
3
3
|
module Constrain
|
4
|
-
# Raised on any error
|
5
|
-
class Error < StandardError; end
|
6
|
-
|
7
4
|
# Raised if types doesn't match a class expression
|
8
|
-
class MatchError <
|
5
|
+
class MatchError < StandardError
|
9
6
|
def initialize(value, exprs, msg = nil, unwind: 0)
|
10
7
|
super msg || "Expected #{value.inspect} to match #{Constrain.fmt_exprs(exprs)}"
|
11
8
|
end
|
@@ -31,7 +28,7 @@ module Constrain
|
|
31
28
|
# constrain(value, *values, unwind: 0)
|
32
29
|
#
|
33
30
|
# Check that value matches one of the class expressions. Raises a
|
34
|
-
# Constrain::
|
31
|
+
# Constrain::ArgumentError if the expression is invalid and a Constrain::MatchError if
|
35
32
|
# the value doesn't match. The exception's backtrace skips :unwind number of
|
36
33
|
# entries
|
37
34
|
def self.constrain(value, *exprs)
|
@@ -39,7 +36,7 @@ module Constrain
|
|
39
36
|
end
|
40
37
|
|
41
38
|
# Return true if the value matches the class expression. Raises a
|
42
|
-
# Constrain::
|
39
|
+
# Constrain::ArgumentError if the expression is invalid
|
43
40
|
def self.constrain?(value, *exprs)
|
44
41
|
do_constrain?(value, *exprs)
|
45
42
|
end
|
@@ -65,10 +62,10 @@ module Constrain
|
|
65
62
|
msg = exprs.pop if exprs.last.is_a?(String)
|
66
63
|
|
67
64
|
begin
|
68
|
-
!exprs.empty? or raise
|
65
|
+
!exprs.empty? or raise ArgumentError, "Empty constraint"
|
69
66
|
exprs.any? { |expr| Constrain.do_constrain_value?(value, expr) } or
|
70
67
|
raise MatchError.new(value, exprs, msg, unwind: unwind)
|
71
|
-
rescue
|
68
|
+
rescue ArgumentError, Constrain::MatchError => ex
|
72
69
|
ex.set_backtrace(caller[1 + unwind..-1])
|
73
70
|
raise
|
74
71
|
end
|
@@ -77,7 +74,7 @@ module Constrain
|
|
77
74
|
|
78
75
|
def self.do_constrain?(value, *exprs)
|
79
76
|
begin
|
80
|
-
!exprs.empty? or raise
|
77
|
+
!exprs.empty? or raise ArgumentError, "Empty constraint"
|
81
78
|
exprs.any? { |expr| Constrain.do_constrain_value?(value, expr) }
|
82
79
|
end
|
83
80
|
end
|
@@ -87,7 +84,7 @@ module Constrain
|
|
87
84
|
when Class, Module
|
88
85
|
value.is_a?(expr)
|
89
86
|
when Array
|
90
|
-
!expr.empty? or raise
|
87
|
+
!expr.empty? or raise ArgumentError, "Empty array in constraint"
|
91
88
|
value.is_a?(Array) && value.all? { |elem| expr.any? { |e| Constrain.constrain?(elem, e) } }
|
92
89
|
when Hash
|
93
90
|
value.is_a?(Hash) && value.all? { |key, value|
|
@@ -117,7 +114,6 @@ module Constrain
|
|
117
114
|
|
118
115
|
# Render a class expression as a String. Same as +expr.inspect+ except that
|
119
116
|
# Proc objects are rendered as "Proc@<sourcefile>>:<linenumber>"
|
120
|
-
#
|
121
117
|
def self.fmt_expr(expr)
|
122
118
|
case expr
|
123
119
|
when Class, Module; expr.to_s
|
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.4.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-
|
11
|
+
date: 2022-03-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: simplecov
|