constrain 0.3.3 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +8 -8
- data/lib/constrain/version.rb +1 -1
- data/lib/constrain.rb +16 -17
- 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: c3983b4e793cdb6dce194d422e4ac27a0781fc4d4956383cb758444100923dc5
|
4
|
+
data.tar.gz: '06029816512b72fca6993cee9dd7fce0a165a5e03d1e7d7292df277dd8cea1bd'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aab2f97456b574a849e5b130f4af473efd755a89a844207f057e10722a32dc664915a1c5e4ed3d03140adec0ddb5cfa7ea15d1fadc9681ec65d4a46d6288de99
|
7
|
+
data.tar.gz: 2f8c4cfe16ae3712aaedab413b5235764a03e6b89dfcb6946d01abbf739537ed0b4d55dde0728e7a58d0569ff6b99e79cb1daec5b6229cd436015d3f20e27d5d
|
data/README.md
CHANGED
@@ -47,16 +47,17 @@ have it available in all child classes
|
|
47
47
|
|
48
48
|
## Methods
|
49
49
|
|
50
|
-
#### constrain(value, \*expressions, message
|
50
|
+
#### constrain(value, \*expressions, message: nil, unwind: 0)
|
51
51
|
|
52
52
|
Return the given value if it matches at least one of the expressions and raise a
|
53
53
|
Constrain::TypeError if not. The value is matched against the expressions using
|
54
54
|
the #=== operator so anything you can put into the 'when' clause of a 'case'
|
55
55
|
statement can be used. #constrain raise a Constrain::MatchError if the value
|
56
|
-
doesn't match any expression
|
56
|
+
doesn't match any expression and an ArgumentError if there is a syntax error in
|
57
|
+
the expression
|
57
58
|
|
58
|
-
The error message can be customized by
|
59
|
-
of backtrace leves can be skipped
|
59
|
+
The error message can be customized by adding the message option and a number
|
60
|
+
of backtrace leves can be skipped using the :unwind option. By default the
|
60
61
|
backtrace will refer to the point of the call of \#constrain. \#constrain
|
61
62
|
raises a Constrain::Error exception if there is an error in the syntax of the
|
62
63
|
class expression
|
@@ -65,9 +66,8 @@ class expression
|
|
65
66
|
want an exception if the parameters doesn't match the expected, but because it
|
66
67
|
returns the value if successful it can be used to check the validity of
|
67
68
|
variables in expressions too, eg. `return constrain(result_of_complex_computation, Integer)`
|
68
|
-
to check the return value of a method
|
69
69
|
|
70
|
-
#### Constrain.constrain(value, \*expressions, message
|
70
|
+
#### Constrain.constrain(value, \*expressions, message: nil, unwind: 0)
|
71
71
|
|
72
72
|
Class method version of #constrain. It is automatically added to classes that
|
73
73
|
include Constrain
|
@@ -77,8 +77,8 @@ include Constrain
|
|
77
77
|
|
78
78
|
It matches value against the class expressions like #constrain but returns true
|
79
79
|
or false as result. It is automatically added to classes that include
|
80
|
-
Constrain. Constrain.constrain? raises a
|
81
|
-
is an error in the syntax of the
|
80
|
+
Constrain. Constrain.constrain? raises a ArgumentError exception if there
|
81
|
+
is an error in the syntax of the expression
|
82
82
|
|
83
83
|
|
84
84
|
## Expressions
|
data/lib/constrain/version.rb
CHANGED
data/lib/constrain.rb
CHANGED
@@ -1,13 +1,10 @@
|
|
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 <
|
9
|
-
def initialize(value, exprs,
|
10
|
-
super
|
5
|
+
class MatchError < StandardError
|
6
|
+
def initialize(value, exprs, message: nil, unwind: 0)
|
7
|
+
super message || "Expected #{value.inspect} to match #{Constrain.fmt_exprs(exprs)}"
|
11
8
|
end
|
12
9
|
end
|
13
10
|
|
@@ -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
|
-
#
|
31
|
+
# 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,14 +36,16 @@ module Constrain
|
|
39
36
|
end
|
40
37
|
|
41
38
|
# Return true if the value matches the class expression. Raises a
|
42
|
-
#
|
39
|
+
# ArgumentError if the expression is invalid
|
43
40
|
def self.constrain?(value, *exprs)
|
44
41
|
do_constrain?(value, *exprs)
|
45
42
|
end
|
46
43
|
|
47
44
|
module ClassMethods
|
48
45
|
# See Constrain.constrain
|
49
|
-
def constrain(*args)
|
46
|
+
def constrain(*args)
|
47
|
+
|
48
|
+
Constrain.do_constrain(*args) end
|
50
49
|
|
51
50
|
# See Constrain.constrain?
|
52
51
|
def constrain?(*args) Constrain.do_constrain?(*args) end
|
@@ -58,17 +57,18 @@ module Constrain
|
|
58
57
|
def self.do_constrain(value, *exprs)
|
59
58
|
if exprs.last.is_a?(Hash)
|
60
59
|
unwind = (exprs.last.delete(:unwind) || 0) + 1
|
61
|
-
|
60
|
+
message = exprs.last.delete(:message)
|
61
|
+
!exprs.last.empty? or exprs.pop # Remove option hash if empty
|
62
62
|
else
|
63
63
|
unwind = 1
|
64
|
+
message = nil
|
64
65
|
end
|
65
|
-
msg = exprs.pop if exprs.last.is_a?(String)
|
66
66
|
|
67
67
|
begin
|
68
|
-
!exprs.empty? or raise
|
68
|
+
!exprs.empty? or raise ArgumentError, "Empty constraint"
|
69
69
|
exprs.any? { |expr| Constrain.do_constrain_value?(value, expr) } or
|
70
|
-
raise MatchError.new(value, exprs,
|
71
|
-
rescue
|
70
|
+
raise MatchError.new(value, exprs, message: message, unwind: unwind)
|
71
|
+
rescue ArgumentError, Constrain::MatchError => ex
|
72
72
|
ex.set_backtrace(caller[1 + unwind..-1])
|
73
73
|
raise
|
74
74
|
end
|
@@ -77,7 +77,7 @@ module Constrain
|
|
77
77
|
|
78
78
|
def self.do_constrain?(value, *exprs)
|
79
79
|
begin
|
80
|
-
!exprs.empty? or raise
|
80
|
+
!exprs.empty? or raise ArgumentError, "Empty constraint"
|
81
81
|
exprs.any? { |expr| Constrain.do_constrain_value?(value, expr) }
|
82
82
|
end
|
83
83
|
end
|
@@ -87,7 +87,7 @@ module Constrain
|
|
87
87
|
when Class, Module
|
88
88
|
value.is_a?(expr)
|
89
89
|
when Array
|
90
|
-
!expr.empty? or raise
|
90
|
+
!expr.empty? or raise ArgumentError, "Empty array in constraint"
|
91
91
|
value.is_a?(Array) && value.all? { |elem| expr.any? { |e| Constrain.constrain?(elem, e) } }
|
92
92
|
when Hash
|
93
93
|
value.is_a?(Hash) && value.all? { |key, value|
|
@@ -117,7 +117,6 @@ module Constrain
|
|
117
117
|
|
118
118
|
# Render a class expression as a String. Same as +expr.inspect+ except that
|
119
119
|
# Proc objects are rendered as "Proc@<sourcefile>>:<linenumber>"
|
120
|
-
#
|
121
120
|
def self.fmt_expr(expr)
|
122
121
|
case expr
|
123
122
|
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.5.1
|
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-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: simplecov
|