Class: Class

Inherits:
Object
  • Object
show all
Defined in:
lib/test/spec/add_allow_switch.rb

Instance Method Summary collapse

Instance Method Details

#add_allow_switch(method, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/test/spec/add_allow_switch.rb', line 4

def add_allow_switch(method, options={})
  default = options[:default] || false
  
  class_eval do
    cattr_accessor "allow_#{method}"
    self.send("allow_#{method}=", default)
    
    alias_method "original_#{method}", method
    
    eval %{
      def #{method}(*args, &block)
        if allow_#{method}
          original_#{method}(*args, &block)
        else
          raise RuntimeError, "You're trying to call `#{method}' on `#{self}', which you probably don't want in a test."
        end
      end
    }, binding, __FILE__, __LINE__
  end
end