constrain 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -4
- data/lib/constrain.rb +60 -11
- data/lib/constrain/version.rb +1 -1
- 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: 21ca1b88c0ffb91b40a2a2335ead91e2570108f8b67eaa9ea7fa3ebe00ada4c4
|
4
|
+
data.tar.gz: '083da1149466231ca8de0dc216cb00c46c540ebc1b7558a0d26422c33b5af9c4'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7b40d638495bf36bd661fd1f8ab7845ec177a3f5cb2f73ddfd400976a9b02f9f8b2f16b2b0301b287cd419c43a2e961e895f96b6e9fd7a46545c96c71a954a28
|
7
|
+
data.tar.gz: 7be37bc44024db00f04d1355cc4bf340ca8728b2ff3014e27bda54f551bab85843f383f61890a8eaf63c65a14907061b3ce49f676732e1b0ad774b0e0e727541
|
data/README.md
CHANGED
@@ -58,7 +58,7 @@ end
|
|
58
58
|
```
|
59
59
|
|
60
60
|
The alternative is to include the constrain Module in a common root class to
|
61
|
-
have it available in all child
|
61
|
+
have it available in all child classes
|
62
62
|
|
63
63
|
## Methods
|
64
64
|
|
@@ -68,7 +68,7 @@ Return the given value if it matches at least one of the class-expressions and r
|
|
68
68
|
Constrain::TypeError if not. The error message can be customized by added the
|
69
69
|
message argument and a number of backtrace leves can be skipped by setting
|
70
70
|
:unwind option. By default the backtrace will refer to the point of the call of
|
71
|
-
\#constrain. \#constrain
|
71
|
+
\#constrain. \#constrain raises a Constrain::Error exception if there is
|
72
72
|
an error in the syntax of the class expression
|
73
73
|
|
74
74
|
\#constrain is typically used to type-check parameters in methods where you
|
@@ -77,13 +77,19 @@ returns the value if successful it can be used to check the validity of
|
|
77
77
|
variables in expressions too, eg. `return constrain(result_of_complex_computation, Integer)`
|
78
78
|
to check the return value of a method
|
79
79
|
|
80
|
+
#### Constrain.constrain(value, \*class-expressions, message = nil, unwind: 0)
|
81
|
+
|
82
|
+
Class method version of #constrain. It is automatically added to classes that
|
83
|
+
include Constrain
|
84
|
+
|
80
85
|
|
81
86
|
#### Constrain.constrain?(value, \*class-expression) -> true or false
|
82
87
|
|
83
88
|
It matches value against the class expressions like #constrain but returns true
|
84
89
|
or false as result and can be used to handle complex type expressions
|
85
|
-
dynamically. It is
|
86
|
-
|
90
|
+
dynamically. It is automatically added to classes that include Constrain.
|
91
|
+
Constrain.constrain? raises a Constrain::Error exception if there is an error
|
92
|
+
in the syntax of the class expression
|
87
93
|
|
88
94
|
## Class Expressions
|
89
95
|
|
data/lib/constrain.rb
CHANGED
@@ -1,4 +1,18 @@
|
|
1
1
|
require "constrain/version"
|
2
|
+
module Foo
|
3
|
+
|
4
|
+
module InstanceMethods
|
5
|
+
def bar1
|
6
|
+
'bar1'
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def bar2
|
12
|
+
'bar2'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
2
16
|
|
3
17
|
module Constrain
|
4
18
|
# Raised on any error
|
@@ -11,23 +25,60 @@ module Constrain
|
|
11
25
|
end
|
12
26
|
end
|
13
27
|
|
28
|
+
def self.included base
|
29
|
+
base.extend ClassMethods
|
30
|
+
end
|
31
|
+
|
32
|
+
# See Constrain.constrain
|
33
|
+
def constrain(value, *exprs)
|
34
|
+
Constrain.do_constrain(value, *exprs)
|
35
|
+
end
|
36
|
+
|
37
|
+
def constrain?(value, expr)
|
38
|
+
Constrain.do_constrain?(value, expr)
|
39
|
+
end
|
40
|
+
|
14
41
|
# :call-seq:
|
15
42
|
# constrain(value, *class-expressions, unwind: 0)
|
16
43
|
#
|
17
44
|
# Check that value matches one of the class expressions. Raises a
|
18
45
|
# Constrain::Error if the expression is invalid and a Constrain::TypeError if
|
19
|
-
# the value doesn't match. The exception's backtrace skips :unwind number of
|
20
|
-
|
46
|
+
# the value doesn't match. The exception's backtrace skips :unwind number of
|
47
|
+
# entries
|
48
|
+
def self.constrain(value, *exprs)
|
49
|
+
do_constrain(value, *exprs)
|
50
|
+
end
|
51
|
+
|
52
|
+
# Return true if the value matches the class expression. Raises a
|
53
|
+
# Constrain::Error if the expression is invalid
|
54
|
+
#
|
55
|
+
# TODO: Allow *exprs
|
56
|
+
def self.constrain?(value, expr)
|
57
|
+
do_constrain?(value, expr)
|
58
|
+
end
|
59
|
+
|
60
|
+
module ClassMethods
|
61
|
+
# See Constrain.constrain
|
62
|
+
def constrain(*args) Constrain.do_constrain(*args) end
|
63
|
+
|
64
|
+
# See Constrain.constrain?
|
65
|
+
def constrain?(*args) Constrain.do_constrain?(*args) end
|
66
|
+
end
|
67
|
+
|
68
|
+
# unwind is automatically incremented by one because ::do_constrain is always
|
69
|
+
# called from one of the other constrain methods
|
70
|
+
#
|
71
|
+
def self.do_constrain(value, *exprs)
|
21
72
|
if exprs.last.is_a?(Hash)
|
22
|
-
unwind = exprs.last.delete(:unwind) || 0
|
73
|
+
unwind = (exprs.last.delete(:unwind) || 0) + 1
|
23
74
|
!exprs.last.empty? or exprs.pop
|
24
75
|
else
|
25
|
-
unwind =
|
76
|
+
unwind = 1
|
26
77
|
end
|
27
78
|
msg = exprs.pop if exprs.last.is_a?(String)
|
28
79
|
begin
|
29
80
|
!exprs.empty? or raise Error, "Empty class expression"
|
30
|
-
exprs.any? { |expr| Constrain.
|
81
|
+
exprs.any? { |expr| Constrain.do_constrain?(value, expr) } or
|
31
82
|
raise TypeError.new(value, exprs, msg, unwind: unwind)
|
32
83
|
rescue Error => ex
|
33
84
|
ex.set_backtrace(caller[1 + unwind..-1])
|
@@ -36,23 +87,21 @@ module Constrain
|
|
36
87
|
value
|
37
88
|
end
|
38
89
|
|
39
|
-
|
40
|
-
# Constrain::Error if the expression is invalid
|
41
|
-
def self.constrain?(value, expr)
|
90
|
+
def self.do_constrain?(value, expr)
|
42
91
|
case expr
|
43
92
|
when Class, Module
|
44
93
|
value.is_a?(expr)
|
45
94
|
when Array
|
46
95
|
!expr.empty? or raise Error, "Empty array"
|
47
|
-
value.is_a?(Array) && value.all? { |elem| expr.any? { |e| constrain?(elem, e) } }
|
96
|
+
value.is_a?(Array) && value.all? { |elem| expr.any? { |e| Constrain.constrain?(elem, e) } }
|
48
97
|
when Hash
|
49
98
|
value.is_a?(Hash) && value.all? { |key, value|
|
50
99
|
expr.any? { |key_expr, value_expr|
|
51
100
|
[[key, key_expr], [value, value_expr]].all? { |value, expr|
|
52
101
|
if expr.is_a?(Array) && (expr.size > 1 || expr.first.is_a?(Array))
|
53
|
-
expr.any? { |e|
|
102
|
+
expr.any? { |e| Constrain.do_constrain?(value, e) }
|
54
103
|
else
|
55
|
-
constrain?(value, expr)
|
104
|
+
Constrain.constrain?(value, expr)
|
56
105
|
end
|
57
106
|
}
|
58
107
|
}
|
data/lib/constrain/version.rb
CHANGED
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.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Claus Rasmussen
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-05-
|
11
|
+
date: 2021-05-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: simplecov
|