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(object, method) ⇒ ClassMethod

Returns a new instance of ClassMethod.



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

def initialize(object, method)
  @object, @method = object, 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

#objectObject (readonly)

Returns the value of attribute object.



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

def object
  @object
end

Instance Method Details

#cannot_replace_method_errorObject



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

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

#define_new_methodObject



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

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

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

Returns:

  • (Boolean)


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

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

#hidden_methodObject



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

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

#hide_original_methodObject



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

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

#remove_new_methodObject



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

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

#restore_original_methodObject



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

def restore_original_method
  object.metaclass.class_eval "alias_method :#{method}, :#{hidden_method}; remove_method :#{hidden_method}" if object.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



55
56
57
# File 'lib/stubba/class_method.rb', line 55

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

#unstubObject



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

def unstub
  remove_new_method
  restore_original_method
  object.reset_mocha
end