Sha256: 4c7ec554d275dca608f8dd67e3a4e8eaebea1d21864ea113dc05ba59e4ad797d

Contents?: true

Size: 964 Bytes

Versions: 3

Compression:

Stored size: 964 Bytes

Contents

module MethodCallbacks
  class Executor
    attr_reader :method, :type, :object

    def initialize(method, type, object)
      @method = method
      @type = type
      @object = object
    end

    def execute(&block)
      case type
      when :proxy
        execute_proxy_callbacks(&block)
      when :around
        execute_around_callbacks(&block)
      else
        execute_callbacks
      end
    end

    private

    def callbacks
      method.callbacks(type)
    end

    def execute_around_callbacks
      callbacks.reverse.reduce(Proc.new) { |block, callback_name| Proc.new { object.send(callback_name, &block) } }.call
    end

    def execute_callbacks
      callbacks.each { |callback_name| callback_name.is_a?(Proc) ? object.instance_eval(&callback_name) : object.send(callback_name) }
    end

    def execute_proxy_callbacks(&block_on_call)
      callbacks.reduce(object) { |result, block| block.call(result, &block_on_call) }
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
method_callbacks-1.2.2 lib/method_callbacks/executor.rb
method_callbacks-1.2.1 lib/method_callbacks/executor.rb
method_callbacks-1.2.0 lib/method_callbacks/executor.rb