Sha256: d2d848b7b28ab972de408563c65a5eb4088e30afb754c7d62424ba153e8b04d5

Contents?: true

Size: 1.07 KB

Versions: 14

Compression:

Stored size: 1.07 KB

Contents

module Aruba
  # Aruba Hooks
  class Hooks
    private

    attr_reader :store

    public

    def initialize
      @store = {}
    end

    # Add new hook
    #
    # @param [String, Symbol] label
    #   The name of the hook
    #
    # @para [Proc] block
    #   The block which should be run for the hook
    def append(label, block)
      if store.key?(label.to_sym) && store[label.to_sym].respond_to?(:<<)
        store[label.to_sym] << block
      else
        store[label.to_sym] = []
        store[label.to_sym] << block
      end
    end

    # Run hook
    #
    # @param [String, Symbol] label
    #   The name of the hook
    #
    # @param [Object] context
    #   The context in which the hook is run
    #
    # @param [Array] args
    #   Other arguments
    def execute(label, context, *args)
      Array(store[label.to_sym]).each do |block|
        context.instance_exec(*args, &block)
      end
    end

    # Check if hook exist
    #
    # @param [String, Symbol] label
    #   The name of the hook
    def exist?(label)
      store.key? label.to_sym
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
aruba-0.11.0.pre lib/aruba/hooks.rb
aruba-0.10.2 lib/aruba/hooks.rb
aruba-0.10.1 lib/aruba/hooks.rb
aruba-0.10.0 lib/aruba/hooks.rb
aruba-0.10.0.pre2 lib/aruba/hooks.rb
aruba-0.10.0.pre lib/aruba/hooks.rb
aruba-0.9.0 lib/aruba/hooks.rb
aruba-0.9.0.pre2 lib/aruba/hooks.rb
aruba-0.9.0.pre lib/aruba/hooks.rb
aruba-0.8.1 lib/aruba/hooks.rb
aruba-0.8.0 lib/aruba/hooks.rb
aruba-0.8.0.pre3 lib/aruba/hooks.rb
aruba-0.8.0.pre2 lib/aruba/hooks.rb
aruba-0.8.0.pre lib/aruba/hooks.rb