Sha256: 71ee4bbe14811feccb0da80e7a61bb46b33e2d63289d10e3a9641f22634acacc

Contents?: true

Size: 606 Bytes

Versions: 1

Compression:

Stored size: 606 Bytes

Contents

# frozen_string_literal: true

module Tamashii
  module Hookable
    # :nodoc:
    class Container < Hash
      def initialize(instance)
        super()
        @instance = instance
      end

      def register(action, name, handler = nil, &block)
        hooks("#{action}_#{name}").push(handler || block)
      end

      def execute(name, *args)
        hooks(name).each do |hook|
          next hook.new(*args).process if hook.is_a?(Class)
          @instance.instance_exec(*args, &hook)
        end
      end

      private

      def hooks(name)
        self[name] ||= []
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tamashii-hookable-0.1.0 lib/tamashii/hookable/container.rb