Class: RR::Injections::SingletonMethodAddedInjection

Inherits:
Injection
  • Object
show all
Includes:
ClassInstanceMethodDefined
Defined in:
lib/rr/injections/singleton_method_added_injection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ClassInstanceMethodDefined

#class_instance_method_defined

Methods inherited from Injection

#original_method, #subject_has_method_defined?, #subject_has_original_method?

Methods included from Space::Reader

#space

Constructor Details

#initialize(subject_class) ⇒ SingletonMethodAddedInjection

Returns a new instance of SingletonMethodAddedInjection.



24
25
26
27
# File 'lib/rr/injections/singleton_method_added_injection.rb', line 24

def initialize(subject_class)
  @subject_class = subject_class
  @placeholder_method_defined = false
end

Instance Attribute Details

#subject_classObject (readonly)

Returns the value of attribute subject_class.



22
23
24
# File 'lib/rr/injections/singleton_method_added_injection.rb', line 22

def subject_class
  @subject_class
end

Instance Method Details

#bindObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rr/injections/singleton_method_added_injection.rb', line 29

def bind
  unless class_instance_method_defined(subject_class, original_method_alias_name, false)
    unless class_instance_method_defined(subject_class, :singleton_method_added, false)
      @placeholder_method_defined = true
      subject_class.class_eval do
        #def singleton_method_added(method_name)
        #  super
        #end
      end
    end

    memoized_original_method_alias_name = original_method_alias_name
    subject_class.__send__(:alias_method, original_method_alias_name, :singleton_method_added)
    memoized_subject_class = subject_class
    memoized_original_method_alias_name = original_method_alias_name
    subject_class.__send__(:define_method, :singleton_method_added) do |method_name_arg|
      if Injections::DoubleInjection.exists?(memoized_subject_class, method_name_arg)
        Injections::DoubleInjection.find_or_create(memoized_subject_class, method_name_arg).send(:deferred_bind_method)
      end
      __send__(memoized_original_method_alias_name, method_name_arg)
    end
  end
  self
end

#resetObject



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rr/injections/singleton_method_added_injection.rb', line 54

def reset
  if subject_has_method_defined?(original_method_alias_name)
    memoized_original_method_alias_name = original_method_alias_name
    placeholder_method_defined = @placeholder_method_defined
    subject_class.class_eval do
      remove_method :singleton_method_added
      unless placeholder_method_defined
        alias_method :singleton_method_added, memoized_original_method_alias_name
      end
      remove_method memoized_original_method_alias_name
    end
  end
end