constrain 0.3.0 → 0.3.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 +4 -4
- data/README.md +1 -1
- data/TODO +5 -2
- data/lib/constrain/version.rb +1 -1
- data/lib/constrain.rb +12 -6
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7128a60ecb2625da98babc6d2e193593b456b1facfe036be66549f6ae7f1c511
|
4
|
+
data.tar.gz: 94bb05e8469ceb9ef8291e0c0f09ef01ce965b7ba73ec426b66ff758f1abe8a5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6526db1c067478b13e834e7f5d74f868ecb39f79faf8454b95f543d6e9ea495d1735b981a6dec28b6eede26f13a4adbaadd2ca02e1125cc01fca7ec6dd313d6a
|
7
|
+
data.tar.gz: e199e4808f5eccdb63d183fc2fdce9cb9d6b258e3ae786c5009da3e4f49e1e3a0d36babb1424e14b92e3a7a3efa1072b49fa7a3d5cbd9b21a597b6eaf83fa901
|
data/README.md
CHANGED
@@ -104,7 +104,7 @@ means you can use regular expressions too:
|
|
104
104
|
|
105
105
|
```ruby
|
106
106
|
# Simple email regular expression (https://stackoverflow.com/a/719543)
|
107
|
-
EMAIL_RE = /^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9
|
107
|
+
EMAIL_RE = /^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9.-]+$/
|
108
108
|
|
109
109
|
def email(address)
|
110
110
|
constrain address, EMAIL_RE
|
data/TODO
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
|
2
|
+
o A tuple method: "Symbol => constrain.tuple(String, Integer)"
|
3
|
+
o An array and hash method: "Symbol => constrain.array(Integer), String => constrain.hash(Symbol, Integer)"
|
2
4
|
o Class | Class syntax
|
3
|
-
o
|
4
|
-
o
|
5
|
+
o Constrained attributes: constrain_reader, constrain_writer, constrain_accessor:
|
6
|
+
o Messages should include info about the unexpected element type in arrays (and maybe more): "Expected [#<PgGraph::Data::Record:public.pings[1] {id: 1, name: 'Ping A'}>, #<PgGraph::Data::Record:public.pings[2] {id: 2, name: 'Ping B'}>, nil] to match [PgGraph::Data::Record]
|
5
7
|
|
6
8
|
+ constrain value, class-expr, "Error message"
|
9
|
+
+ Check against values: 'constrain arg, :one_value, :another_value, 1, 2, 3'
|
data/lib/constrain/version.rb
CHANGED
data/lib/constrain.rb
CHANGED
@@ -40,8 +40,6 @@ module Constrain
|
|
40
40
|
|
41
41
|
# Return true if the value matches the class expression. Raises a
|
42
42
|
# Constrain::Error if the expression is invalid
|
43
|
-
#
|
44
|
-
# TODO: Allow *exprs
|
45
43
|
def self.constrain?(value, expr)
|
46
44
|
do_constrain?(value, expr)
|
47
45
|
end
|
@@ -65,9 +63,10 @@ module Constrain
|
|
65
63
|
unwind = 1
|
66
64
|
end
|
67
65
|
msg = exprs.pop if exprs.last.is_a?(String)
|
66
|
+
|
68
67
|
begin
|
69
|
-
!exprs.empty? or raise Error, "Empty
|
70
|
-
exprs.any? { |expr| Constrain.
|
68
|
+
!exprs.empty? or raise Error, "Empty constraint"
|
69
|
+
exprs.any? { |expr| Constrain.do_constrain_value?(value, expr) } or
|
71
70
|
raise MatchError.new(value, exprs, msg, unwind: unwind)
|
72
71
|
rescue Error => ex
|
73
72
|
ex.set_backtrace(caller[1 + unwind..-1])
|
@@ -76,12 +75,19 @@ module Constrain
|
|
76
75
|
value
|
77
76
|
end
|
78
77
|
|
79
|
-
def self.do_constrain?(value,
|
78
|
+
def self.do_constrain?(value, *exprs)
|
79
|
+
begin
|
80
|
+
!exprs.empty? or raise Error, "Empty constraint"
|
81
|
+
exprs.any? { |expr| Constrain.do_constrain_value?(value, expr) }
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def self.do_constrain_value?(value, expr)
|
80
86
|
case expr
|
81
87
|
when Class, Module
|
82
88
|
value.is_a?(expr)
|
83
89
|
when Array
|
84
|
-
!expr.empty? or raise Error, "Empty array"
|
90
|
+
!expr.empty? or raise Error, "Empty array in constraint"
|
85
91
|
value.is_a?(Array) && value.all? { |elem| expr.any? { |e| Constrain.constrain?(elem, e) } }
|
86
92
|
when Hash
|
87
93
|
value.is_a?(Hash) && value.all? { |key, value|
|
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.3.
|
4
|
+
version: 0.3.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: 2021-
|
11
|
+
date: 2021-10-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: simplecov
|
@@ -71,7 +71,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
71
71
|
- !ruby/object:Gem::Version
|
72
72
|
version: '0'
|
73
73
|
requirements: []
|
74
|
-
rubygems_version: 3.
|
74
|
+
rubygems_version: 3.1.4
|
75
75
|
signing_key:
|
76
76
|
specification_version: 4
|
77
77
|
summary: Dynamic in-file type checking
|