Sha256: 167c41a9be33c2656f85bff1138a233f9a3f9ade5ebe6ccdca26cb740a47500e

Contents?: true

Size: 1.09 KB

Versions: 4

Compression:

Stored size: 1.09 KB

Contents

# Aruba
module Aruba
  # Aruba Hooks
  class Hooks
    private

    attr_reader :store

    public

    # Create store
    def initialize
      @store = {}
    end

    # Add new hook
    #
    # @param [String, Symbol] label
    #   The name of the hook
    #
    # @param [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

4 entries across 4 versions & 1 rubygems

Version Path
aruba-1.0.0 lib/aruba/hooks.rb
aruba-1.0.0.pre.alpha.5 lib/aruba/hooks.rb
aruba-1.0.0.pre.alpha.4 lib/aruba/hooks.rb
aruba-1.0.0.pre.alpha.3 lib/aruba/hooks.rb