Class: Dry::System::Component

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

Overview

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.

Constant Summary collapse

DEFAULT_OPTIONS =
{
  inflector: Dry::Inflector.new,
  loader: Loader
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identifier, file_path:, namespace:, **options) ⇒ 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 new instance of Component.



40
41
42
43
44
45
# File 'lib/dry/system/component.rb', line 40

def initialize(identifier, file_path:, namespace:, **options)
  @identifier = identifier
  @file_path = Pathname(file_path)
  @namespace = namespace
  @options = DEFAULT_OPTIONS.merge(options)
end

Instance Attribute Details

#file_pathObject (readonly)



29
30
31
# File 'lib/dry/system/component.rb', line 29

def file_path
  @file_path
end

#identifierObject (readonly)



25
26
27
# File 'lib/dry/system/component.rb', line 25

def identifier
  @identifier
end

#namespaceObject (readonly)



33
34
35
# File 'lib/dry/system/component.rb', line 33

def namespace
  @namespace
end

#optionsObject (readonly)



37
38
39
# File 'lib/dry/system/component.rb', line 37

def options
  @options
end

Instance Method Details

#auto_register?Boolean

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:

  • (Boolean)


159
160
161
# File 'lib/dry/system/component.rb', line 159

def auto_register?
  callable_option?(options[:auto_register])
end

#const_pathString

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.

Examples:

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

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

Returns:

  • (String)

    the const path

See Also:



138
139
140
141
142
143
144
145
146
# File 'lib/dry/system/component.rb', line 138

def const_path
  namespace_const_path = namespace.const&.gsub(KEY_SEPARATOR, PATH_SEPARATOR)

  if namespace_const_path
    "#{namespace_const_path}#{PATH_SEPARATOR}#{path_in_namespace}"
  else
    path_in_namespace
  end
end

#inflectorObject

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.



154
155
156
# File 'lib/dry/system/component.rb', line 154

def inflector
  options.fetch(:inflector)
end

#instance(*args, **kwargs) ⇒ Object

Returns the component’s instance

Returns:

  • (Object)

    component’s class instance



63
64
65
# File 'lib/dry/system/component.rb', line 63

def instance(*args, **kwargs)
  options[:instance]&.call(self, *args, **kwargs) || loader.call(self, *args, **kwargs)
end

#keyString

Returns the component’s unique key

Returns:

  • (String)

    the key

See Also:



74
75
76
# File 'lib/dry/system/component.rb', line 74

def key
  identifier.key
end

#loadable?TrueClass

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 true, indicating that the component is directly loadable from the files managed by the container

This is the inverse of IndirectComponent#loadable?

Returns:

  • (TrueClass)


55
56
57
# File 'lib/dry/system/component.rb', line 55

def loadable?
  true
end

#loaderObject

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.



149
150
151
# File 'lib/dry/system/component.rb', line 149

def loader
  options.fetch(:loader)
end

#memoize?Boolean

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:

  • (Boolean)


164
165
166
# File 'lib/dry/system/component.rb', line 164

def memoize?
  callable_option?(options[:memoize])
end

#require_pathString

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.

Examples:

Component from a root namespace

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

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

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

Returns:

  • (String)

    the require path

See Also:



108
109
110
111
112
113
114
# File 'lib/dry/system/component.rb', line 108

def require_path
  if namespace.path
    "#{namespace.path}#{PATH_SEPARATOR}#{path_in_namespace}"
  else
    path_in_namespace
  end
end

#root_keySymbol

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

Returns:

  • (Symbol)

    the root key

See Also:



85
86
87
# File 'lib/dry/system/component.rb', line 85

def root_key
  identifier.root_key
end