constrain 0.2.0 → 0.2.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c8c92be44953ded28f60341e340c7e96a8891124427efb31a06cbc94c7664cf1
4
- data.tar.gz: 65196009ea3d69aab1174722296949977bb622a68ac16dd04320dc7816503a2e
3
+ metadata.gz: e48891d02cd81470d1f83543f6db1f7da99d75f3afe05cdc57b2c1e63d7a2ff9
4
+ data.tar.gz: dd37d53ea1080db393871ede639a653f985cc38373314d8b23b5013f3d8f9803
5
5
  SHA512:
6
- metadata.gz: 4acb8bd66add5cb3937efdbe5272b57acac1ea1fe193df84f2ea48c3aa6a12b0a214a2832aeac7e2968871da9c226ba263dfe0ebec883ff42259d9667edfb92f
7
- data.tar.gz: '094404002ee23266e98a58ad7ade0910d0768fa2956440ab1502c768b0fda2785c35d13821e3adb21e571198ed9eb223eac216f04b3406296c34569d71f4a874'
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 can check every aspect of an object but you should not overuse it
143
- because as checks becomes more complex they tend to include business logic that
144
- should be kept in the production code. Constrain is only thouhgt of as a tool
145
- to catch developer errors - not errors that stem from corrupted data
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, unwind: 0)
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"
@@ -1,3 +1,3 @@
1
1
  module Constrain
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: constrain
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen