class Dry::System::Component

Components are objects providing information about auto-registered files. They expose an API to query this information and use a configurable loader object to initialize class instances.

@api public

Constants

DEFAULT_OPTIONS

Attributes

identifier[R]

@!attribute [r] identifier

@return [String] the component's unique identifier
namespace[R]

@!attribute [r] namespace

@return [Dry::System::Config::Namespace] the component's namespace
options[R]

@!attribute [r] options

@return [Hash] the component's options

Public Class Methods

new(identifier, namespace:, **options) click to toggle source

@api private

# File lib/dry/system/component.rb, line 41
def initialize(identifier, namespace:, **options)
  @identifier = identifier
  @namespace = namespace
  @options = DEFAULT_OPTIONS.merge(options)
end

Public Instance Methods

auto_register?() click to toggle source

@api private

# File lib/dry/system/component.rb, line 160
def auto_register?
  callable_option?(options[:auto_register])
end
const_path() click to toggle source

Returns an “underscored”, path-delimited representation of the component, appropriate for passing to the inflector for constantizing

The const path takes into account the rules of the namespace used to load the component.

@example Component from a namespace with `const: nil`

component.key # => "articles.create_article"
component.const_path # => "articles/create_article"
component.inflector.constantize(component.const_path) # => Articles::CreateArticle

@example Component from a namespace with `const: “admin”`

component.key # => "articles.create_article"
component.const_path # => "admin/articles/create_article"
component.inflector.constantize(component.const_path) # => Admin::Articles::CreateArticle

@see Config::Namespaces#add @see Config::Namespace

@return [String] the const path

@api public

# File lib/dry/system/component.rb, line 139
def const_path
  namespace_const_path = namespace.const&.gsub(identifier.separator, PATH_SEPARATOR)

  if namespace_const_path
    "#{namespace_const_path}#{PATH_SEPARATOR}#{path_in_namespace}"
  else
    path_in_namespace
  end
end
inflector() click to toggle source

@api private

# File lib/dry/system/component.rb, line 155
def inflector
  options.fetch(:inflector)
end
instance(*args) click to toggle source

Returns the component's instance

@return [Object] component's class instance @api public

# File lib/dry/system/component.rb, line 63
def instance(*args)
  loader.call(self, *args)
end
key() click to toggle source

Returns the component's unique key

@return [String] the key

@see Identifier#key

@api public

# File lib/dry/system/component.rb, line 75
def key
  identifier.key
end
loadable?() click to toggle source

Returns true, indicating that the component is directly loadable from the files managed by the container

This is the inverse of {IndirectComponent#loadable?}

@return [TrueClass]

@api private

# File lib/dry/system/component.rb, line 55
def loadable?
  true
end
loader() click to toggle source

@api private

# File lib/dry/system/component.rb, line 150
def loader
  options.fetch(:loader)
end
memoize?() click to toggle source

@api private

# File lib/dry/system/component.rb, line 165
def memoize?
  callable_option?(options[:memoize])
end
require_path() click to toggle source

Returns a path-delimited representation of the compnent, appropriate for passing to `Kernel#require` to require its source file

The path takes into account the rules of the namespace used to load the component.

@example Component from a root namespace

component.key # => "articles.create"
component.require_path # => "articles/create"

@example Component from an “admin/” path namespace (with `key: nil`)

component.key # => "articles.create"
component.require_path # => "admin/articles/create"

@see Config::Namespaces#add @see Config::Namespace

@return [String] the require path

@api public

# File lib/dry/system/component.rb, line 109
def require_path
  if namespace.path
    "#{namespace.path}#{PATH_SEPARATOR}#{path_in_namespace}"
  else
    path_in_namespace
  end
end
root_key() click to toggle source

Returns the root namespace segment of the component's key, as a symbol

@see Identifier#root_key

@return [Symbol] the root key

@api public

# File lib/dry/system/component.rb, line 86
def root_key
  identifier.root_key
end

Private Instance Methods

callable_option?(value) click to toggle source
# File lib/dry/system/component.rb, line 182
def callable_option?(value)
  if value.respond_to?(:call)
    !!value.call(self)
  else
    !!value
  end
end
path_in_namespace() click to toggle source
# File lib/dry/system/component.rb, line 171
def path_in_namespace
  identifier_in_namespace =
    if namespace.key
      identifier.namespaced(from: namespace.key, to: nil)
    else
      identifier
    end

  identifier_in_namespace.key_with_separator(PATH_SEPARATOR)
end