Sha256: 795cf9ec2b9cbabf9b3b0045980907b8ddba2afa1d67907f1b6f28025944a5b3

Contents?: true

Size: 1011 Bytes

Versions: 7

Compression:

Stored size: 1011 Bytes

Contents

# -*- encoding: binary -*-

module VTools

  # hooks handler
  # allows to execute hooks from external script
  # multiple hooks in one placeholder are allowed
  #
  # usage:
  #   Handler.set :placeholder_name, &block
  # or
  #   Handler.collection do
  #     set :placeholder_one, &block
  #     set :placeholder_other, &block
  #   end
  class Handler
    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 # Handler
end # VTools

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
vtools-1.0.2 lib/vtools/handler.rb
vtools-1.0.1 lib/vtools/handler.rb
vtools-1.0.0 lib/vtools/handler.rb
vtools-0.1.1 lib/vtools/handler.rb
vtools-0.1.0 lib/vtools/handler.rb
vtools-0.0.3 lib/vtools/handler.rb
vtools-0.0.2 lib/vtools/handler.rb