Class: Module

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


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/test/spec/add_allow_switch.rb', line 27

def add_allow_switch(method, options={})
  default = options[:default] || false
  
  mattr_accessor "allow_#{method}"
  send("allow_#{method}=", default)
  
  unless respond_to?(:__metaclass___)
    def __metaclass__
      class << self; self; end
    end
  end
  
  __metaclass__.class_eval do
    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