constrain 0.4.0 → 0.5.0

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: c76b91e6d73a1c5d2f7c44fc98f372f5b911c08e6a6d4203a3be646019aaa1a3
4
- data.tar.gz: da85fb71b85da4e54913cb227a162c901ef3fe88c61d091e3489a63a3a97a9e5
3
+ metadata.gz: 48a0683db5a9b95baaf0ca8ee0f59322ffadbfe9f8b524a9e6e3d53cf7d1f06c
4
+ data.tar.gz: 3d07bc32462abdd359a85130301a29680f108dfe30f9af43ae1cda10f2a21d7d
5
5
  SHA512:
6
- metadata.gz: 3db8dbbb8a0b2eb8d0c8de705b85533a630748f9e02c4e82f324c8f705d9f01d251b4d9b9866d977b71aaef4beb29f1834f1ff058728055ef545d77ece25a506
7
- data.tar.gz: 52a2ec74e4709506d7be7813b48d5a7ece977dc7314d591db5830033f2e477cab39d2dc26129e7d0d27727f57966b0c0957faf5ba701ef9f6754ca16123f342a
6
+ metadata.gz: fd9457aab6e64780ade38a804b914a3519a5c5f6cabf62cd35f94a5823eced188e2e9e89deb0980319ce6c6d565ebd97456f67b6a0657648226396b7476a0be2
7
+ data.tar.gz: 4f4fcedad8e23c7dcf7a1c52cc69c9f9361fbb95cd3e889c7295c6d0dbc85d67a547a0b9b9e4368d632a122ce1b6b3532a2ef89b00a92ea448a772360a47815a
@@ -1,3 +1,3 @@
1
1
  module Constrain
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
data/lib/constrain.rb CHANGED
@@ -3,8 +3,8 @@ require "constrain/version"
3
3
  module Constrain
4
4
  # Raised if types doesn't match a class expression
5
5
  class MatchError < StandardError
6
- def initialize(value, exprs, msg = nil, unwind: 0)
7
- super msg || "Expected #{value.inspect} to match #{Constrain.fmt_exprs(exprs)}"
6
+ def initialize(value, exprs, message: nil, unwind: 0)
7
+ super message || "Expected #{value.inspect} to match #{Constrain.fmt_exprs(exprs)}"
8
8
  end
9
9
  end
10
10
 
@@ -28,7 +28,7 @@ module Constrain
28
28
  # constrain(value, *values, unwind: 0)
29
29
  #
30
30
  # Check that value matches one of the class expressions. Raises a
31
- # Constrain::ArgumentError if the expression is invalid and a Constrain::MatchError if
31
+ # ArgumentError if the expression is invalid and a Constrain::MatchError if
32
32
  # the value doesn't match. The exception's backtrace skips :unwind number of
33
33
  # entries
34
34
  def self.constrain(value, *exprs)
@@ -36,14 +36,16 @@ module Constrain
36
36
  end
37
37
 
38
38
  # Return true if the value matches the class expression. Raises a
39
- # Constrain::ArgumentError if the expression is invalid
39
+ # ArgumentError if the expression is invalid
40
40
  def self.constrain?(value, *exprs)
41
41
  do_constrain?(value, *exprs)
42
42
  end
43
43
 
44
44
  module ClassMethods
45
45
  # See Constrain.constrain
46
- def constrain(*args) Constrain.do_constrain(*args) end
46
+ def constrain(*args)
47
+
48
+ Constrain.do_constrain(*args) end
47
49
 
48
50
  # See Constrain.constrain?
49
51
  def constrain?(*args) Constrain.do_constrain?(*args) end
@@ -55,16 +57,17 @@ module Constrain
55
57
  def self.do_constrain(value, *exprs)
56
58
  if exprs.last.is_a?(Hash)
57
59
  unwind = (exprs.last.delete(:unwind) || 0) + 1
58
- !exprs.last.empty? or exprs.pop
60
+ message = exprs.last.delete(:message)
61
+ !exprs.last.empty? or exprs.pop # Remove option hash if empty
59
62
  else
60
63
  unwind = 1
64
+ message = nil
61
65
  end
62
- msg = exprs.pop if exprs.last.is_a?(String)
63
66
 
64
67
  begin
65
68
  !exprs.empty? or raise ArgumentError, "Empty constraint"
66
69
  exprs.any? { |expr| Constrain.do_constrain_value?(value, expr) } or
67
- raise MatchError.new(value, exprs, msg, unwind: unwind)
70
+ raise MatchError.new(value, exprs, message: message, unwind: unwind)
68
71
  rescue ArgumentError, Constrain::MatchError => ex
69
72
  ex.set_backtrace(caller[1 + unwind..-1])
70
73
  raise
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.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-03-03 00:00:00.000000000 Z
11
+ date: 2022-03-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: simplecov