Class: Hyrax::Callbacks::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/hyrax/callbacks/registry.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



6
7
8
# File 'lib/hyrax/callbacks/registry.rb', line 6

def initialize
  @callbacks = {}
end

Instance Attribute Details

#callbacksObject (readonly)

Returns the value of attribute callbacks.



4
5
6
# File 'lib/hyrax/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/hyrax/callbacks/registry.rb', line 11

def enable(hook, *more_hooks)
  ([hook] + more_hooks).each { |h| @callbacks[h] ||= nil }
end

#enabledObject

Returns all enabled callback hooks.



16
17
18
# File 'lib/hyrax/callbacks/registry.rb', line 16

def enabled
  @callbacks.keys
end

#enabled?(hook) ⇒ Boolean

Returns true if the callback hook has been enabled.

Returns:

  • (Boolean)


21
22
23
# File 'lib/hyrax/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

Raises:



37
38
39
40
41
# File 'lib/hyrax/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.

Raises:



26
27
28
29
# File 'lib/hyrax/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.

Returns:

  • (Boolean)


32
33
34
# File 'lib/hyrax/callbacks/registry.rb', line 32

def set?(hook)
  enabled?(hook) && @callbacks[hook].respond_to?(:call)
end