lib/constrain.rb in constrain-0.3.3 vs lib/constrain.rb in constrain-0.4.0
- old
+ new
@@ -1,13 +1,10 @@
require "constrain/version"
module Constrain
- # Raised on any error
- class Error < StandardError; end
-
# Raised if types doesn't match a class expression
- class MatchError < Error
+ class MatchError < StandardError
def initialize(value, exprs, msg = nil, unwind: 0)
super msg || "Expected #{value.inspect} to match #{Constrain.fmt_exprs(exprs)}"
end
end
@@ -29,19 +26,19 @@
# :call-seq:
# constrain(value, *class-expressions, unwind: 0)
# constrain(value, *values, unwind: 0)
#
# Check that value matches one of the class expressions. Raises a
- # Constrain::Error if the expression is invalid and a Constrain::MatchError if
+ # Constrain::ArgumentError if the expression is invalid and a Constrain::MatchError if
# the value doesn't match. The exception's backtrace skips :unwind number of
# entries
def self.constrain(value, *exprs)
do_constrain(value, *exprs)
end
# Return true if the value matches the class expression. Raises a
- # Constrain::Error if the expression is invalid
+ # Constrain::ArgumentError if the expression is invalid
def self.constrain?(value, *exprs)
do_constrain?(value, *exprs)
end
module ClassMethods
@@ -63,33 +60,33 @@
unwind = 1
end
msg = exprs.pop if exprs.last.is_a?(String)
begin
- !exprs.empty? or raise Error, "Empty constraint"
+ !exprs.empty? or raise ArgumentError, "Empty constraint"
exprs.any? { |expr| Constrain.do_constrain_value?(value, expr) } or
raise MatchError.new(value, exprs, msg, unwind: unwind)
- rescue Error => ex
+ rescue ArgumentError, Constrain::MatchError => ex
ex.set_backtrace(caller[1 + unwind..-1])
raise
end
value
end
def self.do_constrain?(value, *exprs)
begin
- !exprs.empty? or raise Error, "Empty constraint"
+ !exprs.empty? or raise ArgumentError, "Empty constraint"
exprs.any? { |expr| Constrain.do_constrain_value?(value, expr) }
end
end
def self.do_constrain_value?(value, expr)
case expr
when Class, Module
value.is_a?(expr)
when Array
- !expr.empty? or raise Error, "Empty array in constraint"
+ !expr.empty? or raise ArgumentError, "Empty array in constraint"
value.is_a?(Array) && value.all? { |elem| expr.any? { |e| Constrain.constrain?(elem, e) } }
when Hash
value.is_a?(Hash) && value.all? { |key, value|
expr.any? { |key_expr, value_expr|
[[key, key_expr], [value, value_expr]].all? { |value, expr|
@@ -115,10 +112,9 @@
exprs.map { |expr| fmt_expr(expr) }.join(", ")
end
# Render a class expression as a String. Same as +expr.inspect+ except that
# Proc objects are rendered as "Proc@<sourcefile>>:<linenumber>"
- #
def self.fmt_expr(expr)
case expr
when Class, Module; expr.to_s
when Regexp; expr.to_s
when Array; "[" + expr.map { |expr| fmt_expr(expr) }.join(", ") + "]"