Class: Hyrax::Callbacks::Registry
- Inherits:
-
Object
- Object
- Hyrax::Callbacks::Registry
- Defined in:
- app/services/hyrax/callbacks.rb
Instance Attribute Summary collapse
-
#callbacks ⇒ Object
readonly
Returns the value of attribute callbacks.
Instance Method Summary collapse
-
#enable(hook, *more_hooks) ⇒ Object
Enables a callback by specifying one or more hooks.
-
#enabled ⇒ Object
Returns all enabled callback hooks.
-
#enabled?(hook) ⇒ Boolean
Returns true if the callback hook has been enabled.
-
#initialize ⇒ Registry
constructor
A new instance of Registry.
-
#run(hook, *args, warn: true, **opts) ⇒ Object
Runs the callback defined for a given hook, with the arguments provided.
-
#set(hook, warn: true, &block) ⇒ Object
Defines a callback for a given hook.
-
#set?(hook) ⇒ Boolean
Returns true if a callback has been defined for a given hook.
Constructor Details
#initialize ⇒ Registry
Returns a new instance of Registry.
30 31 32 |
# File 'app/services/hyrax/callbacks.rb', line 30 def initialize @callbacks = {} end |
Instance Attribute Details
#callbacks ⇒ Object (readonly)
Returns the value of attribute callbacks.
28 29 30 |
# File 'app/services/hyrax/callbacks.rb', line 28 def callbacks @callbacks end |
Instance Method Details
#enable(hook, *more_hooks) ⇒ Object
Enables a callback by specifying one or more hooks.
35 36 37 |
# File 'app/services/hyrax/callbacks.rb', line 35 def enable(hook, *more_hooks) ([hook] + more_hooks).each { |h| @callbacks[h] ||= nil } end |
#enabled ⇒ Object
Returns all enabled callback hooks.
40 41 42 |
# File 'app/services/hyrax/callbacks.rb', line 40 def enabled @callbacks.keys end |
#enabled?(hook) ⇒ Boolean
Returns true if the callback hook has been enabled.
45 46 47 |
# File 'app/services/hyrax/callbacks.rb', line 45 def enabled?(hook) @callbacks.key? hook end |
#run(hook, *args, warn: true, **opts) ⇒ Object
Runs the callback defined for a given hook, with the arguments provided
62 63 64 65 66 67 |
# File 'app/services/hyrax/callbacks.rb', line 62 def run(hook, *args, warn: true, **opts) Deprecation.warn(self, warning_for_run) if warn raise NotEnabled unless enabled?(hook) return nil unless set?(hook) @callbacks[hook].call(*args, **opts) end |
#set(hook, warn: true, &block) ⇒ Object
Defines a callback for a given hook.
50 51 52 53 54 |
# File 'app/services/hyrax/callbacks.rb', line 50 def set(hook, warn: true, &block) Deprecation.warn(self, warning_for_set) if warn raise NoBlockGiven, "a block is required when setting a callback" unless block_given? @callbacks[hook] = proc(&block) end |
#set?(hook) ⇒ Boolean
Returns true if a callback has been defined for a given hook.
57 58 59 |
# File 'app/services/hyrax/callbacks.rb', line 57 def set?(hook) enabled?(hook) && @callbacks[hook].respond_to?(:call) end |