Module: Bang::MethodMissing

Included in:
Object
Defined in:
lib/bang.rb

Overview

Mixin of Object class, will take any undefined bang method, e.g. foo! and if there is a corresponding query method, e.g. foo?, then it will utilize the query method to make an assertion.

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(s, *a, &b) ⇒ Object

If missing method is a bang method, see if there is a corresponding query method and use that to make an assertion. Will also recognize the same prefixed by not_, e.g. not_equal_to?.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/bang.rb', line 73

def method_missing(s, *a, &b)
  return super(s, *a, &b) unless s.to_s.end_with?('!')

  neg  = false
  name = s.to_s.chomp('!')

  if name.start_with?('not_')
    neg  = true
    name = name[4..-1]
  end

  meth = method("#{name}?") rescue nil

  return super(s, *a, &b) unless meth

  result = meth.call(*a, &b)

  if neg
    refute(result, Bang::Assertion.piece(s, a, b, caller))
  else
    assert(result, Bang::Assertion.piece(s, a, b, caller))
  end
end