Module: SuperCallbacks::InheritancePrepender

Defined in:
lib/super_callbacks.rb

Instance Method Summary collapse

Instance Method Details

#inherited(subclass) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/super_callbacks.rb', line 34

def inherited(subclass)
  # need to make a copy of the last SuperCallbacks::Prepended module in the ancestor chain
  # and then `prepend` that module into the newly defined subclass
  first_callbacks_prepended_module_instance = self.ancestors.detect { |ancestor| ancestor.is_a? SuperCallbacks::Prepended }

  new_super_callbacks_prepended = Prepended.new(subclass)

  subclass.singleton_class.send :attr_accessor, :super_callbacks_prepended
  subclass.super_callbacks_prepended = new_super_callbacks_prepended

  # ... which could be done via redefining the methods first...
  (
    first_callbacks_prepended_module_instance.instance_methods(false) +
    first_callbacks_prepended_module_instance.private_instance_methods(false)
  ).each do |method_name|
    new_super_callbacks_prepended.send(:define_method, method_name) do |*args|
      # this is the reason why this method needs to be redefined, and not just simply
      # copied from the last SuperCallbacks::Prepended module;
      # because the callback must only run once on the outermost prepended module
      # for details, see spec 'runs the callbacks in correct order when the method is defined in the subclass'
      return super(*args) if self.class.super_callbacks_prepended != subclass.super_callbacks_prepended

      begin
        # refactored to use Thread.current for thread-safetiness
        Thread.current[:super_callbacks_all_instance_variables_before_change] ||= {}
        Thread.current[:super_callbacks_all_instance_variables_before_change][object_id] ||= []

        all_instance_variables_before_change = instance_variables.each_with_object({}) do |instance_variable, hash|
          hash[instance_variable] = instance_variable_get(instance_variable)
        end

        Thread.current[:super_callbacks_all_instance_variables_before_change][object_id] << all_instance_variables_before_change

        run_before_callbacks(method_name, *args)
        super_value = super(*args)
        run_after_callbacks(method_name, *args)
      ensure
        Thread.current[:super_callbacks_all_instance_variables_before_change][object_id].pop

        if Thread.current[:super_callbacks_all_instance_variables_before_change][object_id].empty?
          Thread.current[:super_callbacks_all_instance_variables_before_change].delete(object_id)
        end

        if Thread.current[:super_callbacks_all_instance_variables_before_change].empty?
          Thread.current[:super_callbacks_all_instance_variables_before_change] = nil
        end
      end

      super_value
    end
  end

  subclass.send :prepend, new_super_callbacks_prepended

  copied_before_callbacks = Helpers.deep_array_and_hash_dup(first_callbacks_prepended_module_instance.base.before_callbacks)
  copied_after_callbacks = Helpers.deep_array_and_hash_dup(first_callbacks_prepended_module_instance.base.after_callbacks)

  subclass.instance_variable_set(:@before_callbacks, copied_before_callbacks)
  subclass.instance_variable_set(:@after_callbacks, copied_after_callbacks)

  # subclass.singleton_class.send :prepend, InheritancePrepender
  super
end