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