Sha256: 0ab134b76f4c9b830f378cd0fc8b558f59003264896954374676025c4b848dfa

Contents?: true

Size: 991 Bytes

Versions: 3

Compression:

Stored size: 991 Bytes

Contents

# -*- encoding: binary -*-

module VTools

  # hooks
  # allows to execute hooks from external script
  # multiple hooks in one placeholder are allowed
  #
  # usage:
  #   Hook.set :placeholder_name, &block
  # or
  #   Hook.collection do
  #     set :placeholder_one, &block
  #     set :placeholder_other, &block
  #   end
  class Hook
    include SharedMethods

    @callbacks = {}

    class << self
      # hooks setter
      def set action, &block
        action = action.to_sym
        @callbacks[action] = [] unless @callbacks[action].is_a? Array
        @callbacks[action] << block if block_given?
      end

      # pending hooks exectuion
      def exec action, *args
        action = action.to_sym
        @callbacks[action].each do |block|
          block.call(*args)
        end if @callbacks[action].is_a? Array
      end

      # collection setup
      def collection &block
        instance_eval &block if block_given?
      end
    end # << self
  end # Hook
end # VTools

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
vtools-1.0.2 lib/vtools/hook.rb
vtools-1.0.1 lib/vtools/hook.rb
vtools-1.0.0 lib/vtools/hook.rb