Sha256: deea10c239b472cc87e3c9bca4262ec317a3a4ceccd6d692540d639bc2e2562b

Contents?: true

Size: 1.44 KB

Versions: 61

Compression:

Stored size: 1.44 KB

Contents

require 'mocha/metaclass'

module Mocha

  class ClassMethod
  
    attr_reader :stubbee, :method
   
    def initialize(stubbee, method)
      @stubbee, @method = stubbee, method
    end
  
    def stub
      hide_original_method
      define_new_method
    end
  
    def unstub
      remove_new_method
      restore_original_method
      stubbee.reset_mocha
    end
    
    def mock
      stubbee.mocha
    end
  
    def hide_original_method
      stubbee.__metaclass__.class_eval "alias_method :#{hidden_method}, :#{method}" if stubbee.__metaclass__.method_defined?(method)
    end
  
    def define_new_method
      stubbee.__metaclass__.class_eval "def #{method}(*args, &block); mocha.method_missing(:#{method}, *args, &block); end"
    end
  
    def remove_new_method
      stubbee.__metaclass__.class_eval "remove_method :#{method}"
    end
  
    def restore_original_method
      stubbee.__metaclass__.class_eval "alias_method :#{method}, :#{hidden_method}; remove_method :#{hidden_method}" if stubbee.__metaclass__.method_defined?(hidden_method)
    end
  
    def hidden_method
      method_name = method.to_s.gsub(/\W/) {|s| "_substituted_character_#{s[0]}_" }
      "__stubba__#{method_name}__stubba__"
    end  
  
    def eql?(other)
      return false unless (other.class == self.class)
      (stubbee == other.stubbee) and (method == other.method)
    end
  
    alias_method :==, :eql?
  
    def to_s
      "#{stubbee}.#{method}"
    end

  end
  
end

Version data entries

61 entries across 61 versions & 6 rubygems

Version Path
greatseth-google-geo-2.0 vendor/mocha-0.4.0/lib/mocha/class_method.rb
greatseth-google-geo-2.1 vendor/mocha-0.4.0/lib/mocha/class_method.rb
greatseth-google-geo-2.2 vendor/mocha-0.4.0/lib/mocha/class_method.rb
redcar-dev-0.12.1dev-java lib/openssl/mocha/lib/mocha/class_method.rb
redcar-dev-0.12.0dev-java lib/openssl/mocha/lib/mocha/class_method.rb
redcar-0.11 lib/openssl/mocha/lib/mocha/class_method.rb
redcar-0.11.0dev lib/openssl/mocha/lib/mocha/class_method.rb
redcar-0.10 lib/openssl/mocha/lib/mocha/class_method.rb
redcar-0.9.2 lib/openssl/mocha/lib/mocha/class_method.rb
redcar-0.9.1 lib/openssl/mocha/lib/mocha/class_method.rb
redcar-0.9.0 lib/openssl/mocha/lib/mocha/class_method.rb
redcar-0.8.1 lib/openssl/mocha/lib/mocha/class_method.rb
redcar-0.8 lib/openssl/mocha/lib/mocha/class_method.rb
redcar-0.7 lib/openssl/mocha/lib/mocha/class_method.rb
redcar-0.6.1 lib/openssl/mocha/lib/mocha/class_method.rb
redcar-0.6 lib/openssl/mocha/lib/mocha/class_method.rb
redcar-0.6.1dev lib/openssl/mocha/lib/mocha/class_method.rb
redcar-0.5.1 lib/openssl/mocha/lib/mocha/class_method.rb
redcar-0.5 lib/openssl/mocha/lib/mocha/class_method.rb
redcar-0.5.6dev lib/openssl/mocha/lib/mocha/class_method.rb