Module: Callmeback

Extended by:
ActiveSupport::Concern
Includes:
ActiveSupport::Callbacks
Defined in:
lib/callmeback.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#callmeback!Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/callmeback.rb', line 8

def callmeback!
  self.class.callmeback_methods.each do |callback_prefix, callback_array|
    callback_array.each do |callback_hash|
      callback_hash.each do |callback_key, callbacks|

        callbacks = [callbacks].flatten

        if callback_key.is_a?(Regexp)
          binded_methods = methods.select{|m| m =~ callback_key}
        else
          binded_methods = [callback_key].flatten
        end

        binded_methods.each do |binded|
          callbacks.each do |callback|

            binded_suffix = :"method_#{self.class.callmeback_method_index}"
            self.class.callmeback_method_index += 1

            prefixed_wrapped_binded = :"callmeback_wrapped_#{binded_suffix}"
            prefixed_unwrapped_binded = :"callmeback_unwrapped_#{binded_suffix}"

            class_eval do
              define_method prefixed_wrapped_binded do
                class_eval do
                  define_callbacks prefixed_wrapped_binded
                  set_callback prefixed_wrapped_binded, callback_prefix, callback
                end

                run_callbacks prefixed_wrapped_binded do
                  send prefixed_unwrapped_binded
                end
              end

              alias_method prefixed_unwrapped_binded, binded
              alias_method binded, prefixed_wrapped_binded
            end
          end
        end
      end
    end
  end
end