Module: Attr::Gather::Registrable

Included in:
Aggregators, Filters
Defined in:
lib/attr/gather/concerns/registrable.rb

Overview

Makes a module registrable

Defined Under Namespace

Classes: AlreadyRegisteredError, NotFoundError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ Object



13
14
15
# File 'lib/attr/gather/concerns/registrable.rb', line 13

def self.extended(klass)
  klass.instance_variable_set(:@__storage__, {})
end

Instance Method Details

#ensure_name_not_already_registered!(name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



42
43
44
45
46
47
# File 'lib/attr/gather/concerns/registrable.rb', line 42

def ensure_name_not_already_registered!(name)
  return unless @__storage__.key?(name)

  raise AlreadyRegisteredError,
        "item with name #{name} already registered"
end

#register(name) {|options| ... } ⇒ Object

Register item so it can be accessed by name

Parameters:

  • name (Symbol)

    name of the item

Yields:

  • (options)

    block to initialize the item



21
22
23
24
25
# File 'lib/attr/gather/concerns/registrable.rb', line 21

def register(name, &blk)
  name = name.to_sym

  @__storage__[name] = blk
end

#resolve(name, *args, **opts) ⇒ #call

Resolve a named item

Parameters:

  • name (Symbol)

Returns:

  • (#call)


32
33
34
35
36
37
38
39
# File 'lib/attr/gather/concerns/registrable.rb', line 32

def resolve(name, *args, **opts)
  block = @__storage__.fetch(name) do
    raise NotFoundError,
          "no item with name #{name} registered"
  end

  block.call(*args, **opts)
end