constrain 0.4.0 → 0.6.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/.ruby-version +1 -1
- data/README.md +8 -8
- data/constrain.gemspec +0 -1
- data/lib/constrain/version.rb +1 -1
- data/lib/constrain.rb +11 -8
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2f6fc2db2a7997351f2df9185e2283863d53c53b806c2f888f0cba637530d62b
|
4
|
+
data.tar.gz: 96ecb2679c16da578365a499bdfe393dd4c9ff72e4121139b18b35d00102019f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fdd94bbb07066064239fd7b137d06b7e43465b6c72b80dafc419d846956c11b23c87f2dab3c32fd3e0a27ea45aeafeb00c1bde712ffd61d33f0c53d90c700fdd
|
7
|
+
data.tar.gz: 8c4bce76f79ce7aa93743f7ea33b899ad19a17b3e590e1a2ec9204ec0cc51b33be3d756d070963e3f752326cc3021608b12023611b54c2b92c4f79e125e859a8
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
ruby-
|
1
|
+
ruby-3.1.2
|
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/constrain.gemspec
CHANGED
@@ -27,7 +27,6 @@ Gem::Specification.new do |spec|
|
|
27
27
|
Constrain works with ruby-2 (and maybe ruby-3)
|
28
28
|
}
|
29
29
|
spec.homepage = "https://github.com/clrgit/constrain/"
|
30
|
-
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
31
30
|
|
32
31
|
spec.metadata["homepage_uri"] = spec.homepage
|
33
32
|
|
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.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Claus Rasmussen
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-07-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: simplecov
|
@@ -56,7 +56,7 @@ homepage: https://github.com/clrgit/constrain/
|
|
56
56
|
licenses: []
|
57
57
|
metadata:
|
58
58
|
homepage_uri: https://github.com/clrgit/constrain/
|
59
|
-
post_install_message:
|
59
|
+
post_install_message:
|
60
60
|
rdoc_options: []
|
61
61
|
require_paths:
|
62
62
|
- lib
|
@@ -64,15 +64,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
64
64
|
requirements:
|
65
65
|
- - ">="
|
66
66
|
- !ruby/object:Gem::Version
|
67
|
-
version:
|
67
|
+
version: '0'
|
68
68
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
69
|
requirements:
|
70
70
|
- - ">="
|
71
71
|
- !ruby/object:Gem::Version
|
72
72
|
version: '0'
|
73
73
|
requirements: []
|
74
|
-
rubygems_version: 3.
|
75
|
-
signing_key:
|
74
|
+
rubygems_version: 3.3.18
|
75
|
+
signing_key:
|
76
76
|
specification_version: 4
|
77
77
|
summary: Dynamic in-file type checking
|
78
78
|
test_files: []
|