Sha256: 91c808df896243a8dca632a8dcfe94d8d46ed243d1a22afebff7d25569f680b5

Contents?: true

Size: 1.72 KB

Versions: 5

Compression:

Stored size: 1.72 KB

Contents

require 'class_inheritable_attributes'

module AssetCloud
  module Callbacks
    extend ActiveSupport::Concern

    module ClassMethods
      def callback_methods(*symbols)
        symbols.each do |method|
          code = <<-"end_eval"
           def self.before_#{method}(*callbacks, &block)
             callbacks << block if block_given?
             write_inheritable_array(:before_#{method}, callbacks)
           end

           def self.after_#{method}(*callbacks, &block)
             callbacks << block if block_given?
             write_inheritable_array(:after_#{method}, callbacks)
           end

           def #{method}_with_callbacks(*args)
             if execute_callbacks(:before_#{method}, args)
               result = #{method}_without_callbacks(*args)
               execute_callbacks(:after_#{method}, args)
             end
             result
           end

           alias_method_chain :#{method}, 'callbacks'
          end_eval

          self.class_eval code, __FILE__, __LINE__
        end
      end
    end

    def execute_callbacks(symbol, args)
      callbacks_for(symbol).each do |callback|

        result = case callback
        when Symbol
          self.send(callback, *args)
        when Proc, Method
          callback.call(self, *args)
        else
          if callback.respond_to?(method)
            callback.send(method, self, *args)
          else
            raise StandardError, "Callbacks must be a symbol denoting the method to call, a string to be evaluated, a block to be invoked, or an object responding to the callback method."
          end
        end
        return false if result == false
      end
      true
    end

    def callbacks_for(symbol)
      self.class.send(symbol) || []
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
asset_cloud-2.2.4 lib/asset_cloud/callbacks.rb
asset_cloud-2.2.2 lib/asset_cloud/callbacks.rb
asset_cloud-2.2.1 lib/asset_cloud/callbacks.rb
asset_cloud-2.2.0 lib/asset_cloud/callbacks.rb
asset_cloud-2.1.0 lib/asset_cloud/callbacks.rb