Sha256: c58e719f8b548b568d6aabe196edfdfd20106e7bf398978373beb7c513fc6422

Contents?: true

Size: 932 Bytes

Versions: 5

Compression:

Stored size: 932 Bytes

Contents

module Plate
  module Callbacks
    def self.included(base)
      base.send(:include, InstanceMethods)
      base.send(:extend, ClassMethods)
    end
    
    module ClassMethods
      def callbacks
        @callbacks ||= {}
      end
      
      def register_callback(name, method_name = nil, &block)
        callbacks[name] ||= []
        callbacks[name] << (block || method_name)
      end
    end
    
    module InstanceMethods
      def around_callback(name, &block)
        run_callback "before_#{name}".to_sym
        block.call
        run_callback "after_#{name}".to_sym
      end
      
      def run_callback(name)
        if callbacks = self.class.callbacks[name]
          callbacks.each do |callback|
            if Proc === callback
              callback.call(self)
            elsif self.respond_to?(callback)
              self.send(callback)
            end
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
plate-0.5.4 lib/plate/callbacks.rb
plate-0.5.3 lib/plate/callbacks.rb
plate-0.5.2 lib/plate/callbacks.rb
plate-0.5.1 lib/plate/callbacks.rb
plate-0.5.0 lib/plate/callbacks.rb