Class: Gimme::SpiesOnClassMethod

Inherits:
Object
  • Object
show all
Defined in:
lib/gimme/spies_on_class_methods.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cls) ⇒ SpiesOnClassMethod

Returns a new instance of SpiesOnClassMethod.



4
5
6
7
# File 'lib/gimme/spies_on_class_methods.rb', line 4

def initialize(cls)
  @cls = cls
  @raises_no_method_error = true
end

Instance Attribute Details

#raises_no_method_errorObject

Returns the value of attribute raises_no_method_error.



3
4
5
# File 'lib/gimme/spies_on_class_methods.rb', line 3

def raises_no_method_error
  @raises_no_method_error
end

Instance Method Details

#spy(method) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/gimme/spies_on_class_methods.rb', line 9

def spy(method)
  meta_class = (class << @cls; self; end)
  method = ResolvesMethods.new(meta_class, method).resolve(@raises_no_method_error)

  if meta_class.method_defined? method
    Gimme.class_methods.set(@cls, method)
    meta_class.send(:remove_method, method)
  end

  cls = @cls
  meta_class.instance_eval do
    define_method method do |*args|
      Gimme.invocations.increment(cls, method, args)
    end
  end

  EnsuresClassMethodRestoration.new(@cls).ensure(method)
end