Module: Hanami::Components Private

Defined in:
lib/hanami/components.rb,
lib/hanami/components/app/view.rb,
lib/hanami/components/component.rb,
lib/hanami/components/app/assets.rb,
lib/hanami/components/app/routes.rb,
lib/hanami/components/components.rb,
lib/hanami/components/app/controller.rb,
lib/hanami/components/routes_inspector.rb

Overview

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

Since:

  • 0.9.0

Defined Under Namespace

Modules: App Classes: Component, RoutesInspector

Class Method Summary collapse

Class Method Details

.[](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.

Return the value of an already resolved component. Or raise error for not resolved component.

Examples:

Hanami::Components.resolved('repository.users') { UserRepository.new }

Hanami::Components['repository.users'] # => #<UserRepository relations=[...]>
Hanami::Components['repository.other'] # => error

Raises:

  • (ArgumentError)

    if the component is unknown or not resolved yet.

Since:

  • 0.9.0



127
128
129
130
131
# File 'lib/hanami/components.rb', line 127

def self.[](name)
  @_resolved.fetch(name) do
    raise ArgumentError.new("Component not resolved: `#{name}'.\nResolved components are: #{@_resolved.keys.join(', ')}")
  end
end

.component(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.

Return a registered component

Raises:

  • (ArgumentError)

    if the component is unknown

Since:

  • 0.9.0



70
71
72
73
74
# File 'lib/hanami/components.rb', line 70

def self.component(name)
  @_components.fetch(name) do
    raise ArgumentError.new("Component not found: `#{name}'.\nAvailable components are: #{@_components.keys.join(', ')}")
  end
end

.register(name, &blk) ⇒ 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.

Register a component

See Also:

Since:

  • 0.9.0



58
59
60
# File 'lib/hanami/components.rb', line 58

def self.register(name, &blk)
  @_components[name] = Component.new(name, &blk)
end

.releaseObject

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.

Release all the resolved components. This is used for code reloading.

NOTE: this MUST NOT be used unless you know what you're doing.

Examples:

Hanami::Components.resolved('repository.users') { UserRepository.new }
Hanami::Components['repository.users'] # => #<UserRepository relations=[...]>

Hanami::Components.release
Hanami::Components['repository.users']
# => ArgumentError: Component not resolved: `repo'.
# => Resolved components are: ...

Since:

  • 1.0.0



149
150
151
# File 'lib/hanami/components.rb', line 149

def self.release
  @_resolved.clear
end

.resolve(*names) ⇒ 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.

Ask to resolve a component.

This is used as dependency mechanism. For instance `model` component depends on `model.configuration`. Before to resolve `model`, `Components` uses this method to resolve that dependency first.

Since:

  • 0.9.0



104
105
106
107
108
109
110
111
# File 'lib/hanami/components.rb', line 104

def self.resolve(*names)
  Array(names).flatten.each do |name|
    @_resolved.fetch_or_store(name) do
      component = @_components.fetch(name)
      component.call(Hanami.configuration)
    end
  end
end

.resolved(name, value = nil, &blk) ⇒ 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.

Mark a component as resolved by providing a value or a block. In the latter case, the returning value of the block is associated with the component.

Since:

  • 0.9.0



86
87
88
89
90
91
92
# File 'lib/hanami/components.rb', line 86

def self.resolved(name, value = nil, &blk)
  if block_given?
    @_resolved.fetch_or_store(name, &blk)
  else
    @_resolved.compute_if_absent(name) { value }
  end
end