Module: Hammock::Callbacks::ClassMethods

Defined in:
lib/hammock/callbacks.rb

Instance Method Summary collapse

Instance Method Details

#define_hammock_callbacks(*callbacks) ⇒ Object



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

def define_hammock_callbacks *callbacks
  callbacks.each do |callback|
    class_eval <<-"end_eval"
      def self.#{callback}(*methods, &block)
        callbacks = if !block_given? || methods.length > 1
          CallbackChain.build(:#{callback}, *methods, &block)
        else # hammock-style callback
          if methods.empty?
            log "<-- you should give this callback a description", :skip => 1
          elsif !methods.first.is_a?(String)
            raise ArgumentError, "Inline callback definitions require a description as their sole argument."
          else
            # logger.info "defining \#{methods.first} on \#{name} with method \#{block.inspect}."
            [Hammock::Callback.new(:#{callback}, block, :identifier => methods.first)]
          end || []
        end
        # log callbacks
        (@#{callback}_callbacks ||= CallbackChain.new).concat callbacks
      end

      def self.#{callback}_callback_chain
        @#{callback}_callbacks ||= CallbackChain.new

        if superclass.respond_to?(:#{callback}_callback_chain)
          CallbackChain.new(superclass.#{callback}_callback_chain + @#{callback}_callbacks)
        else
          @#{callback}_callbacks
        end
      end
    end_eval
  end
end