Class: Dry::System::ComponentDir Private

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/system/component_dir.rb

Overview

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

A configured component directory within the container’s root. Provides access to the component directory’s configuration, as well as methods for locating component files within the directory

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:, container:) ⇒ ComponentDir

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.

Returns a new instance of ComponentDir.



26
27
28
29
# File 'lib/dry/system/component_dir.rb', line 26

def initialize(config:, container:)
  @config = config
  @container = container
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)

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.



158
159
160
161
162
163
164
# File 'lib/dry/system/component_dir.rb', line 158

def method_missing(name, *args, &block)
  if config.respond_to?(name)
    config.public_send(name, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#configObject (readonly)

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.



18
19
20
# File 'lib/dry/system/component_dir.rb', line 18

def config
  @config
end

#containerObject (readonly)

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.



23
24
25
# File 'lib/dry/system/component_dir.rb', line 23

def container
  @container
end

Instance Method Details

#component_for_key(key) ⇒ Dry::System::Component?

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.

Returns a component for the given key if a matching source file is found within the component dir

This searches according to the component dir’s configured namespaces, in order of definition, with the first match returned as the component.

Parameters:

  • key (String)

    the component’s key

Returns:



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dry/system/component_dir.rb', line 41

def component_for_key(key)
  config.namespaces.each do |namespace|
    identifier = Identifier.new(key)

    next unless identifier.start_with?(namespace.key)

    if (file_path = find_component_file(identifier, namespace))
      return build_component(identifier, namespace, file_path)
    end
  end

  nil
end

#each_componentObject

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.



55
56
57
58
59
60
61
# File 'lib/dry/system/component_dir.rb', line 55

def each_component
  return enum_for(:each_component) unless block_given?

  each_file do |file_path, namespace|
    yield component_for_path(file_path, namespace)
  end
end