Class: Hyrax::Callbacks::Registry

Inherits:
Object
  • Object
show all
Defined in:
app/services/hyrax/callbacks.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



30
31
32
# File 'app/services/hyrax/callbacks.rb', line 30

def initialize
  @callbacks = {}
end

Instance Attribute Details

#callbacksObject (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

#enabledObject

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.

Returns:

  • (Boolean)


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

Raises:



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.

Raises:



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.

Returns:

  • (Boolean)


57
58
59
# File 'app/services/hyrax/callbacks.rb', line 57

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