Sha256: a7a5af1dd54f58d5504149e8246787a7b603cf303fb347f3ff4ea61f73e0c9c5

Contents?: true

Size: 1.86 KB

Versions: 5

Compression:

Stored size: 1.86 KB

Contents

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'.
  #
  # THIS IS AN OPTIONAL LIBRARY.
  #
  module Subjunctive

    #
    #def not(&block)
    #  @negated = !@negated
    #  return self if !block_given?
    #  be(*args, &block)
    #end

    # Like #assert, except if an argument if 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_given? && 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, excpet 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_given? && 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 Assertor
  include AE::Subjunctive
end

# Copyright (c) 2008,2009 Thomas Sawyer

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ae-1.2.3 lib/ae/subjunctive.rb
ae-1.2.2 lib/ae/subjunctive.rb
ae-1.2 lib/ae/subjunctive.rb
ae-1.1.0 lib/ae/subjunctive.rb
ae-1.0.0 lib/ae/subjunctive.rb