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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fda0b95a5d43c197c9df1134d68dbe4fbc1f8f71ad9dc7a8e995daf729eaea2c
4
- data.tar.gz: 2fe722e5085bc9b396e203b3f3514a1f123c18843e293f902392519ef6a3d5ed
3
+ metadata.gz: 7128a60ecb2625da98babc6d2e193593b456b1facfe036be66549f6ae7f1c511
4
+ data.tar.gz: 94bb05e8469ceb9ef8291e0c0f09ef01ce965b7ba73ec426b66ff758f1abe8a5
5
5
  SHA512:
6
- metadata.gz: bc98410262f21b5fea23182cad7c9e80e48a4aff7ff64dc7ff2113581440a191bfa9b77a28045624caf4f6e3102251e175841604c64461287ea7eb1062174bf2
7
- data.tar.gz: c19a11b5ab9e22b49ad2204b276658d26b109296815e06298620fdb53104f6ce70625095b4ac5b3fbd3aff79664866de7afb3507cea1b5d1737aaf714c902c7e
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 attr_reader :variable, ClassExpr
4
- o 'constrain arg, :one_value, :another_value, 1, 2, 3'
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'
@@ -1,3 +1,3 @@
1
1
  module Constrain
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
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 class expression"
70
- exprs.any? { |expr| Constrain.do_constrain?(value, expr) } or
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, expr)
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.0
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-09-20 00:00:00.000000000 Z
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.2.26
74
+ rubygems_version: 3.1.4
75
75
  signing_key:
76
76
  specification_version: 4
77
77
  summary: Dynamic in-file type checking