Module: Rubydora::Callbacks::ExtendableClassMethods

Defined in:
lib/rubydora/callbacks.rb

Overview

hooks support

Instance Method Summary collapse

Instance Method Details

#hooksObject

creates the @hooks container (“hooks” are blocks or procs). returns an array



16
17
18
# File 'lib/rubydora/callbacks.rb', line 16

def hooks
  @hooks ||= {}
end

#register_callback(*attrs) ⇒ Object

register callback procs

Parameters:

  • hook (Array<Symbol>)

    name



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
# File 'lib/rubydora/callbacks.rb', line 22

def register_callback(*attrs)
  attrs.each do |method_name|
    next if methods.include? method_name.to_s
    instance_eval "
      def #{method_name}(&blk)
        self.hooks[:#{method_name}] ||= []
        self.hooks[:#{method_name}] << blk
      end

      def clear_#{method_name}_blocks!
        self.hooks[:#{method_name}] = []
      end
    "

    class_eval "
      def call_#{method_name}
        self.class.hooks[:#{method_name}] ||= []
        self.class.hooks[:#{method_name}].each do |h|
          instance_eval &h
        end
      end

    "
  end
end