Class: Stubba::ClassMethod

Inherits:
Object show all
Defined in:
lib/stubba/class_method.rb

Direct Known Subclasses

AnyInstanceMethod, InstanceMethod

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stubbee, method) ⇒ ClassMethod

Returns a new instance of ClassMethod.



9
10
11
# File 'lib/stubba/class_method.rb', line 9

def initialize(stubbee, method)
  @stubbee, @method = stubbee, method
end

Instance Attribute Details

#methodObject (readonly)

Returns the value of attribute method.



7
8
9
# File 'lib/stubba/class_method.rb', line 7

def method
  @method
end

#stubbeeObject (readonly)

Returns the value of attribute stubbee.



7
8
9
# File 'lib/stubba/class_method.rb', line 7

def stubbee
  @stubbee
end

Instance Method Details

#cannot_replace_method_errorObject



48
49
50
# File 'lib/stubba/class_method.rb', line 48

def cannot_replace_method_error
  Test::Unit::AssertionFailedError.new("Cannot replace #{method} because it is not defined in #{stubbee}.")
end

#define_new_methodObject



32
33
34
# File 'lib/stubba/class_method.rb', line 32

def define_new_method
  stubbee.metaclass.class_eval "def #{method}(*args, &block); mocha.method_missing(:#{method}, *args, &block); end"
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


52
53
54
55
# File 'lib/stubba/class_method.rb', line 52

def eql?(other)
  return false unless (other.class == self.class)
  (stubbee == other.stubbee) and (method == other.method)
end

#hidden_methodObject



44
45
46
# File 'lib/stubba/class_method.rb', line 44

def hidden_method
  "__stubba__#{method.to_s.sub('!', '_exclamation_mark').sub('?', '_question_mark')}__stubba__"
end

#hide_original_methodObject



28
29
30
# File 'lib/stubba/class_method.rb', line 28

def hide_original_method
  stubbee.metaclass.class_eval "alias_method :#{hidden_method}, :#{method}" if stubbee.metaclass.method_defined?(method)
end

#mockObject



24
25
26
# File 'lib/stubba/class_method.rb', line 24

def mock
  stubbee.mocha
end

#remove_new_methodObject



36
37
38
# File 'lib/stubba/class_method.rb', line 36

def remove_new_method
  stubbee.metaclass.class_eval "remove_method :#{method}"
end

#restore_original_methodObject



40
41
42
# File 'lib/stubba/class_method.rb', line 40

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

#stubObject



13
14
15
16
# File 'lib/stubba/class_method.rb', line 13

def stub
  hide_original_method
  define_new_method
end

#to_sObject



59
60
61
# File 'lib/stubba/class_method.rb', line 59

def to_s
  "#{stubbee}.#{method}"
end

#unstubObject



18
19
20
21
22
# File 'lib/stubba/class_method.rb', line 18

def unstub
  remove_new_method
  restore_original_method
  stubbee.reset_mocha
end