Class: CurationConcerns::Callbacks::Registry
- Inherits:
-
Object
- Object
- CurationConcerns::Callbacks::Registry
- Defined in:
- lib/curation_concerns/callbacks/registry.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) ⇒ Object
Runs the callback defined for a given hook, with the arguments provided.
-
#set(hook, &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.
6 7 8 |
# File 'lib/curation_concerns/callbacks/registry.rb', line 6 def initialize @callbacks = {} end |
Instance Attribute Details
#callbacks ⇒ Object (readonly)
Returns the value of attribute callbacks.
4 5 6 |
# File 'lib/curation_concerns/callbacks/registry.rb', line 4 def callbacks @callbacks end |
Instance Method Details
#enable(hook, *more_hooks) ⇒ Object
Enables a callback by specifying one or more hooks.
11 12 13 |
# File 'lib/curation_concerns/callbacks/registry.rb', line 11 def enable(hook, *more_hooks) ([hook] + more_hooks).each { |h| @callbacks[h] ||= nil } end |
#enabled ⇒ Object
Returns all enabled callback hooks.
16 17 18 |
# File 'lib/curation_concerns/callbacks/registry.rb', line 16 def enabled @callbacks.keys end |
#enabled?(hook) ⇒ Boolean
Returns true if the callback hook has been enabled.
21 22 23 |
# File 'lib/curation_concerns/callbacks/registry.rb', line 21 def enabled?(hook) @callbacks.key? hook end |
#run(hook, *args) ⇒ Object
Runs the callback defined for a given hook, with the arguments provided
37 38 39 40 41 |
# File 'lib/curation_concerns/callbacks/registry.rb', line 37 def run(hook, *args) raise NotEnabled unless enabled?(hook) return nil unless set?(hook) @callbacks[hook].call(*args) end |
#set(hook, &block) ⇒ Object
Defines a callback for a given hook.
26 27 28 29 |
# File 'lib/curation_concerns/callbacks/registry.rb', line 26 def set(hook, &block) 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.
32 33 34 |
# File 'lib/curation_concerns/callbacks/registry.rb', line 32 def set?(hook) enabled?(hook) && @callbacks[hook].respond_to?(:call) end |