Class: Assertor
- Includes:
- AE::Subjunctive
- Defined in:
- lib/ae/assertor.rb,
lib/ae/subjunctive.rb
Overview
module AE
Instance Method Summary collapse
-
#=~(match) ⇒ Object
Ruby seems to have a quark in it’s implementation whereby this must be defined explicitly, otherwise it somehow skips #method_missing.
-
#assert(*args, &block) ⇒ Object
Internal assert, provides all functionality accosicated with external #assert Object method.
- #flunk(msg = nil) ⇒ Object
-
#initialize(delegate, opts = {}) ⇒ Assertor
constructor
New Assertor.
- #not(msg = nil, &block) ⇒ Object
Methods included from AE::Subjunctive
Constructor Details
#initialize(delegate, opts = {}) ⇒ Assertor
New Assertor.
19 20 21 22 23 24 |
# File 'lib/ae/assertor.rb', line 19 def initialize(delegate, opts={}) #, backtrace) @delegate = delegate @message = opts[:message] @backtrace = opts[:backtrace] || caller #[1..-1] @negated = !!opts[:negated] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *a, &b) ⇒ Object (private)
Converts a missing methods into an Assertion.
80 81 82 83 84 85 86 87 |
# File 'lib/ae/assertor.rb', line 80 def method_missing(sym, *a, &b) pass = @delegate.__send__(sym, *a, &b) #if (@negated ? pass : !pass) unless @negated ^ pass msg = @message || __msg__(sym, *a, &b) flunk(msg) #fail Assertion.new(msg, :backtrace=>@backtrace) end end |
Instance Method Details
#=~(match) ⇒ Object
Ruby seems to have a quark in it’s implementation whereby this must be defined explicitly, otherwise it somehow skips #method_missing.
72 73 74 |
# File 'lib/ae/assertor.rb', line 72 def =~(match) method_missing(:"=~", match) end |
#assert(*args, &block) ⇒ Object
Internal assert, provides all functionality accosicated with external #assert Object method.
NOTE: I’m calling YAGNI on any extra arguments to the block.
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/ae/assertor.rb', line 37 def assert(*args, &block) return self if args.empty? && !block_given? 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 # truthiness msg = args.shift end __assert__(pass, msg) end |
#flunk(msg = nil) ⇒ Object
65 66 67 |
# File 'lib/ae/assertor.rb', line 65 def flunk(msg=nil) fail Assertion.new(msg || @message, :backtrace=>@backtrace) end |
#not(msg = nil, &block) ⇒ Object
27 28 29 30 |
# File 'lib/ae/assertor.rb', line 27 def not(msg=nil, &block) @negated = !@negated block ? assert(msg, &block) : self end |