Class: Decidim::BlockRegistry

Inherits:
Object
  • Object
show all
Defined in:
decidim-core/lib/decidim/block_registry.rb

Overview

This class acts as a registry for any code blocks with names and should be used as a singleton through the ‘Decidim` core module. This allows registering code blocks for different use cases, for example for the authorization transfers.

Instance Method Summary collapse

Instance Method Details

#register(name) { ... } ⇒ Proc

Register a code block with a name and the handler block.

Parameters:

  • name (Symbol)

    The key for the block.

Yields:

  • The block to be registered for the provided key.

Returns:

  • (Proc)

    The registered block itself.



22
23
24
25
26
# File 'decidim-core/lib/decidim/block_registry.rb', line 22

def register(name, &block)
  return unless block_given?

  registrations[name] = block
end

#registrationsHash<Symbol => Proc>

Provides access to the registered blocks with their names.

Returns:

  • (Hash<Symbol => Proc>)

    A hash of the currently registered blocks with their keys as the hash keys and the blocks as the hash values.



13
14
15
# File 'decidim-core/lib/decidim/block_registry.rb', line 13

def registrations
  @registrations ||= {}
end

#unregister(name) ⇒ Proc #unregister(*names) ⇒ Array<Proc>

Overloads:

  • #unregister(name) ⇒ Proc

    Unregister a single registered handler with the provided name.

    Parameters:

    • name (Symbol)

      The name of the registered block.

    Returns:

    • (Proc)

      The originally registered block for the given key.

  • #unregister(*names) ⇒ Array<Proc>

    Unregister registered handlers with the provided names.

    Parameters:

    • names (Array<Symbol>)

      The names of the registered blocks to be unregistered.

    Returns:

    • (Array<Proc>)

      An array of the originally registered blocks.



37
38
39
40
41
42
43
44
# File 'decidim-core/lib/decidim/block_registry.rb', line 37

def unregister(*names)
  blocks = names.map do |name|
    registrations.delete(name)
  end
  return blocks.first if names.length == 1

  blocks
end