Sha256: 6983131e8188e3e648859b158327ccef489f47ce6d5faaac7c5ca2e9775f68f0

Contents?: true

Size: 1.26 KB

Versions: 66

Compression:

Stored size: 1.26 KB

Contents

module CurationConcerns
  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

66 entries across 66 versions & 1 rubygems

Version Path
curation_concerns-1.7.8 lib/curation_concerns/callbacks/registry.rb
curation_concerns-1.7.7 lib/curation_concerns/callbacks/registry.rb
curation_concerns-2.0.0 lib/curation_concerns/callbacks/registry.rb
curation_concerns-2.0.0.rc2 lib/curation_concerns/callbacks/registry.rb
curation_concerns-1.7.6 lib/curation_concerns/callbacks/registry.rb
curation_concerns-1.7.5 lib/curation_concerns/callbacks/registry.rb
curation_concerns-1.7.4 lib/curation_concerns/callbacks/registry.rb
curation_concerns-1.7.3 lib/curation_concerns/callbacks/registry.rb
curation_concerns-1.7.2 lib/curation_concerns/callbacks/registry.rb
curation_concerns-2.0.0.rc1 lib/curation_concerns/callbacks/registry.rb
curation_concerns-1.7.1 lib/curation_concerns/callbacks/registry.rb
curation_concerns-1.7.0 lib/curation_concerns/callbacks/registry.rb
curation_concerns-1.7.0.beta1 lib/curation_concerns/callbacks/registry.rb
curation_concerns-1.6.3 lib/curation_concerns/callbacks/registry.rb
curation_concerns-1.6.2 lib/curation_concerns/callbacks/registry.rb
curation_concerns-1.6.1 lib/curation_concerns/callbacks/registry.rb
curation_concerns-1.6.0 lib/curation_concerns/callbacks/registry.rb
curation_concerns-1.5.0 lib/curation_concerns/callbacks/registry.rb
curation_concerns-1.4.0 lib/curation_concerns/callbacks/registry.rb
curation_concerns-1.3.3 lib/curation_concerns/callbacks/registry.rb