Sha256: a6b8f511b45a6ec34932099fa3105153e30db2aff34354ee72bcff3de1eef0b2

Contents?: true

Size: 1.25 KB

Versions: 58

Compression:

Stored size: 1.25 KB

Contents

module Hyrax
  module Callbacks
    class Registry
      attr_reader :callbacks

      def initialize
        @callbacks = {}
      end

      # Enables a callback by specifying one or more hooks.
      def enable(hook, *more_hooks)
        ([hook] + more_hooks).each { |h| @callbacks[h] ||= nil }
      end

      # Returns all enabled callback hooks.
      def enabled
        @callbacks.keys
      end

      # Returns true if the callback hook has been enabled.
      def enabled?(hook)
        @callbacks.key? hook
      end

      # Defines a callback for a given hook.
      def set(hook, &block)
        raise NoBlockGiven, "a block is required when setting a callback" unless block_given?
        @callbacks[hook] = proc(&block)
      end

      # Returns true if a callback has been defined for a given hook.
      def set?(hook)
        enabled?(hook) && @callbacks[hook].respond_to?(:call)
      end

      # Runs the callback defined for a given hook, with the arguments provided
      def run(hook, *args)
        raise NotEnabled unless enabled?(hook)
        return nil unless set?(hook)
        @callbacks[hook].call(*args)
      end
    end

    # Custom exceptions
    class NotEnabled < StandardError; end
    class NoBlockGiven < StandardError; end
  end
end

Version data entries

58 entries across 58 versions & 2 rubygems

Version Path
hyrax-2.9.6 lib/hyrax/callbacks/registry.rb
hyrax-2.9.5 lib/hyrax/callbacks/registry.rb
hyrax-2.9.4 lib/hyrax/callbacks/registry.rb
hyrax-2.9.3 lib/hyrax/callbacks/registry.rb
hyrax-2.9.2 lib/hyrax/callbacks/registry.rb
hyrax-2.9.1 lib/hyrax/callbacks/registry.rb
hyrax-2.9.0 lib/hyrax/callbacks/registry.rb
hyrax-2.8.0 lib/hyrax/callbacks/registry.rb
hyrax-2.7.2 lib/hyrax/callbacks/registry.rb
hyrax-2.7.1 lib/hyrax/callbacks/registry.rb
hyrax-2.7.0 lib/hyrax/callbacks/registry.rb
hyrax-2.6.0 lib/hyrax/callbacks/registry.rb
hyrax-3.0.0.pre.rc1 lib/hyrax/callbacks/registry.rb
hyrax-3.0.0.pre.beta3 lib/hyrax/callbacks/registry.rb
hyrax-2.5.1 lib/hyrax/callbacks/registry.rb
hyrax-2.5.0 lib/hyrax/callbacks/registry.rb
hyrax-3.0.0.pre.beta2 lib/hyrax/callbacks/registry.rb
hyrax-2.4.1 lib/hyrax/callbacks/registry.rb
hyrax-3.0.0.pre.beta1 lib/hyrax/callbacks/registry.rb
hyrax-2.4.0 lib/hyrax/callbacks/registry.rb