Class: Assay::RSpec::Operatics

Inherits:
BasicObject
Defined in:
lib/assay/rspec/operatics.rb

Overview

Operatics delegates operator based assertions for execpt.rb.

It is built on BasicObject, so that a word method such as kind_of? is an error rather than being answered by this object.

Instance Method Summary collapse

Constructor Details

#initialize(target, negate = false) ⇒ Operatics

Returns a new instance of Operatics.



12
13
14
15
# File 'lib/assay/rspec/operatics.rb', line 12

def initialize(target, negate=false)
  @target = target
  @negate = negate
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(op, *a, &b) ⇒ Object (private)



20
21
22
23
24
25
26
27
28
# File 'lib/assay/rspec/operatics.rb', line 20

def method_missing(op, *a, &b)
  super(op, *a, &b) if op.to_s =~ /^\w/

  if @negate
    refute!(op, *a, &b)
  else
    assert!(op, *a, &b)
  end
end

Instance Method Details

#assert!(op, *a, &b) ⇒ Object (private)



31
32
33
34
35
36
37
# File 'lib/assay/rspec/operatics.rb', line 31

def assert!(op, *a, &b)
  if assay = ::Assertion.by_operator(op)
    return assay.assert!(@target, *a, {:backtrace=>::Kernel.caller}, &b)
  else
    ::Kernel.assert(@target.__send__(op, *a, &b), ::Assertion, "#{@target.inspect} #{op} #{a.map{ |x| x.inspect }.join(', ')}", ::Kernel.caller)
  end
end

#refute!(op, *a, &b) ⇒ Object (private)



40
41
42
43
44
45
46
# File 'lib/assay/rspec/operatics.rb', line 40

def refute!(op, *a, &b)
  if assay = ::Assertion.by_operator(op)
    return assay.refute!(@target, *a, {:backtrace=>::Kernel.caller}, &b)
  else
    ::Kernel.refute(@target.__send__(op, *a, &b), ::Assertion, "! #{@target.inspect} #{op} #{a.map{ |x| x.inspect }.join(', ')}", ::Kernel.caller)
  end
end