constrain 0.2.0 → 0.2.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 +9 -4
- data/lib/constrain.rb +10 -1
- data/lib/constrain/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e48891d02cd81470d1f83543f6db1f7da99d75f3afe05cdc57b2c1e63d7a2ff9
|
4
|
+
data.tar.gz: dd37d53ea1080db393871ede639a653f985cc38373314d8b23b5013f3d8f9803
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz: '
|
6
|
+
metadata.gz: f2b8bf75caa5936d24773c662f4f43da95820c7245770a1b07c80e82e397aaeea06927e749fb9e1d615daebee3be92ccfa0001ebf334a1d4ceab19d87c225c3c
|
7
|
+
data.tar.gz: '082302ac6cbd3a638f4175a2a91cb3e6bb73e75032d08e9eb21c1e41fff122a3909c983bdf1068bf383b1c32fd93e91808abc687301e446f9c4ef0b05ab2b21e'
|
data/README.md
CHANGED
@@ -139,10 +139,15 @@ constrain 0, Integer # Success
|
|
139
139
|
constrain 0, lambda { |value| value > 1 } # Failure
|
140
140
|
```
|
141
141
|
|
142
|
-
Proc objects
|
143
|
-
|
144
|
-
|
145
|
-
|
142
|
+
Proc objects are a little more verbose than checking the constraint without
|
143
|
+
\#constrain but it allows the use of the :unwind option to manipulate the
|
144
|
+
apparent origin in the source of the exception
|
145
|
+
|
146
|
+
Note that even though Proc objects can check every aspect of an object but you
|
147
|
+
should not overuse it because as checks becomes more complex they tend to
|
148
|
+
include business logic that should be kept in the production code. Constrain is
|
149
|
+
only thouhgt of as a tool to catch developer errors - not errors that stem from
|
150
|
+
corrupted data
|
146
151
|
|
147
152
|
#### Arrays
|
148
153
|
|
data/lib/constrain.rb
CHANGED
@@ -11,10 +11,19 @@ module Constrain
|
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
|
+
# :call-seq:
|
15
|
+
# constrain(value, *class-expressions, unwind: 0)
|
16
|
+
#
|
14
17
|
# Check that value matches one of the class expressions. Raises a
|
15
18
|
# Constrain::Error if the expression is invalid and a Constrain::TypeError if
|
16
19
|
# the value doesn't match. The exception's backtrace skips :unwind number of entries
|
17
|
-
def constrain(value, *exprs
|
20
|
+
def constrain(value, *exprs) #, unwind: 0)
|
21
|
+
if exprs.last.is_a?(Hash)
|
22
|
+
unwind = exprs.last.delete(:unwind) || 0
|
23
|
+
!exprs.last.empty? or exprs.pop
|
24
|
+
else
|
25
|
+
unwind = 0
|
26
|
+
end
|
18
27
|
msg = exprs.pop if exprs.last.is_a?(String)
|
19
28
|
begin
|
20
29
|
!exprs.empty? or raise Error, "Empty class expression"
|
data/lib/constrain/version.rb
CHANGED