Module: Hanami::Components Private

Defined in:
lib/hanami/components.rb,
lib/hanami/commands/apps.rb,
lib/hanami/components/app/view.rb,
lib/hanami/components/component.rb,
lib/hanami/components/app/assets.rb,
lib/hanami/components/app/logger.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.

Registered components

See Also:

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.

Parameters:

  • name (String)

    the component name

Raises:

  • (ArgumentError)

    if the component is unknown or not resolved yet.

Since:

  • 0.9.0


98
99
100
101
102
# File 'lib/hanami/components.rb', line 98

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

Parameters:

  • name (String)

    the name of the component

Raises:

  • (ArgumentError)

    if the component is unknown

Since:

  • 0.9.0


47
48
49
50
51
# File 'lib/hanami/components.rb', line 47

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

Parameters:

  • name (String)

    the unique component name

  • blk (Proc)

    the logic of the component

See Also:

Since:

  • 0.9.0


35
36
37
# File 'lib/hanami/components.rb', line 35

def self.register(name, &blk)
  @_components[name] = Component.new(name, &blk)
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.

Parameters:

  • names (String, Array<String>)

    one or more components to be resolved

Since:

  • 0.9.0


81
82
83
84
85
86
87
88
# File 'lib/hanami/components.rb', line 81

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.

Parameters:

  • name (String)

    the name of the component to mark as resolved

  • value (Object) (defaults to: nil)

    the optional value of the component

  • blk (Proc)

    the optional block which returning value is associated with the component.

Since:

  • 0.9.0


63
64
65
66
67
68
69
# File 'lib/hanami/components.rb', line 63

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