1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
require 'ae/assertor'
module AE
# Subjunctive
#
# Mixin for Assertor that provides additional English-eque verbage
# such as 'be' and 'an'. This makes it easier to create assertor
# methods of subjunctive terms like 'should'.
#
# @note THIS IS AN OPTIONAL LIBRARY.
module Subjunctive
# Like #assert, except if an argument is provided and no block,
# uses #equate? to compare the argument to the receiver. This
# allows for statements of the form:
#
# 5.should.be Numeric
#
def be(*args, &block)
return self if args.empty? && !block
block = args.shift if !block && ::Proc === args.first
if block
pass = block.arity > 0 ? block.call(@delegate) : block.call #@delegate.instance_eval(&block)
msg = args.shift || @message || block.inspect
else
pass = args.shift.equate?(@delegate)
msg = args.shift
end
__assert__(pass, msg)
end
# Alias of #be.
#
# 5.assert.is Numeric
#
alias_method :is , :be
alias_method :does, :be
# The indefinite article is like #be, except that it compares a lone argument
# with #case?, rather than #equate?
#
def a(*args, &block)
return self if args.empty? && !block
block = args.shift if !block && ::Proc === args.first
if block
pass = block.arity > 0 ? block.call(@delegate) : block.call #@delegate.instance_eval(&block)
msg = args.shift || @message || block.inspect
else
pass = (args.shift === @delegate) # case equality
msg = args.shift
end
__assert__(pass, msg)
end
alias_method :an, :a
end
end#module AE
class AE::Assertor
include ::AE::Subjunctive
end
# Copyright (c) 2008 Thomas Sawyer
|