Sha256: ccf9fd304da96cd7413f9781caa804a16e2aaf7854295d57c1ddc1975c65d8bc

Contents?: true

Size: 829 Bytes

Versions: 155

Compression:

Stored size: 829 Bytes

Contents

module Common
  module Hooks
    HOOKS = [:after_load]

    HOOKS.each do |hook_name|
      # define instance-level registration method per hook
      define_method hook_name do |callable = nil, &block|
        callable ||= block
        Hooks.callbacks.fetch(hook_name) << callable if callable
      end

      # define singleton-level run_* method per hook
      define_singleton_method "run_#{hook_name}" do |*args|
        callbacks.fetch(hook_name).each do |callback|
          callback.call(*args)
        end
      end
    end

    # beware, the callbacks hash is a single shared instance tied to this module
    def self.callbacks
      @callbacks ||= initialize_callbacks_array_per_hook
    end

    private

    def self.initialize_callbacks_array_per_hook
      Hash[HOOKS.map { |name| [name, []] }]
    end
  end
end

Version data entries

155 entries across 155 versions & 1 rubygems

Version Path
axe-core-api-4.10.1.pre.5294583 lib/hooks.rb
axe-core-api-4.10.2 lib/hooks.rb
axe-core-api-4.10.2.pre.2cca230 lib/hooks.rb
axe-core-api-4.10.1.pre.46a0052 lib/hooks.rb
axe-core-api-4.10.1.pre.7e72b19 lib/hooks.rb
axe-core-api-4.10.1.pre.5e15f96 lib/hooks.rb
axe-core-api-4.10.2.pre.5bd4e5c lib/hooks.rb
axe-core-api-4.10.1.pre.7bf959f lib/hooks.rb
axe-core-api-4.10.1 lib/hooks.rb
axe-core-api-4.10.0.pre.53569f6 lib/hooks.rb
axe-core-api-4.10.1.pre.7a3723a lib/hooks.rb
axe-core-api-4.10.0.pre.d102edb lib/hooks.rb
axe-core-api-4.9.1.pre.7a03090 lib/hooks.rb
axe-core-api-4.9.1.pre.1a5838a lib/hooks.rb
axe-core-api-4.10.0 lib/hooks.rb
axe-core-api-4.10.0.pre.5d7eac5 lib/hooks.rb
axe-core-api-4.9.1.pre.f16172e lib/hooks.rb
axe-core-api-4.9.1.pre.09e659e lib/hooks.rb
axe-core-api-4.9.1.pre.a80eeee lib/hooks.rb
axe-core-api-4.9.0.pre.a02013a lib/hooks.rb