Class: Stubborn::ProxyForInstance

Inherits:
Object
  • Object
show all
Defined in:
lib/stubborn/proxy_for_instance.rb

Direct Known Subclasses

ProxyForModule

Instance Method Summary collapse

Constructor Details

#initialize(proxy_target, options = {}) ⇒ ProxyForInstance

Returns a new instance of ProxyForInstance.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/stubborn/proxy_for_instance.rb', line 9

def initialize(proxy_target, options = {})
  @proxy_target = proxy_target

  options[:except] = [options[:except]].flatten.compact.map{|m| m.to_s}
  options[:only] = [options[:only]].flatten.compact.map{|m| m.to_s}
  options = {:class => proxy_target.class}.merge(options)
  @label = options[:label]
  @class = options[:class]
  @methods_to_skip = ["respond_to?", "is_a?", "kind_of?", "equal?", "eql?", "==", "==="] + options[:except]
  @only_methods = options[:only]
  @instance_methods = options[:instance_methods]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/stubborn/proxy_for_instance.rb', line 26

def method_missing(method_name, *args, &block)
  were_we_already_processing_a_missed_stub = Thread.current["inside_missed_stub"]
  Thread.current["inside_missed_stub"] = true
  result = begin
             @proxy_target.send(method_name, *args, &block)
           rescue => e
             e
           end

  return result if !@only_methods.include?(method_name.to_s) && !@only_methods.empty? || @methods_to_skip.include?(method_name.to_s) || were_we_already_processing_a_missed_stub
  raise_missed_stub_exception(method_name, args, result)
ensure
  Thread.current["inside_missed_stub"] = false unless were_we_already_processing_a_missed_stub
end

Instance Method Details

#classObject



22
23
24
# File 'lib/stubborn/proxy_for_instance.rb', line 22

def class
  @class
end