class Dry::Rails::Container

Customized Container class for Rails applications

@api public

Public Class Methods

auto_register!(*paths, set_load_paths: true, load_files: false, &block) click to toggle source

Set up auto-registration paths and optional a configuration block

@example set up a single path

Dry::Rails.container do
  auto_register!("app/operations")
end

@example set up a single path with a configuration block

Dry::Rails.container do
  auto_register!("app/operations") do |config|
    config.exclude do |component|
      component.path.start_with?("concerns")
    end
  end
end

@example set up multiple paths

Dry::Rails.container do
  auto_register!("lib", "app/operations")
end

@example set up multiple paths with a configuration block

Dry::Rails.container do
  # in this case the config block will be applied to all paths
  auto_register!("lib", "app/operations") do |config|
    config.exclude do |component|
      component.path.start_with?("concerns")
    end
  end
end

@param paths [Array<String>] One or more paths relative to the root @param set_load_paths [Boolean] Whether the paths should be added to $LOAD_PATH @param load_files [Boolean] Whether files should be `required`-ed already

@return [self]

@api public

TODO: this should be moved to dry-system

Calls superclass method
# File lib/dry/rails/container.rb, line 99
def auto_register!(*paths, set_load_paths: true, load_files: false, &block)
  load_paths!(*paths) if set_load_paths

  if load_files
    paths.each { |path| super(path, &block) }
  else
    config.auto_register_paths.concat(paths.product([block]))
  end

  self
end
booted?(name) click to toggle source

Return if a given component was booted

@return [Boolean]

@api private

TODO: this should be moved to dry-system

# File lib/dry/rails/container.rb, line 142
def booted?(name)
  booter.booted.map(&:identifier).include?(name)
end
finalize!(freeze: false, &block) click to toggle source

Finalize the container

This is called automatically via the railtie, so typically you won't be using this method directly

@param freeze [Boolean] Whether the container should be frozen upon finalization

@return [self]

@api public

TODO: just like auto_register!, this should be moved to dry-system

Calls superclass method
# File lib/dry/rails/container.rb, line 123
def finalize!(freeze: false, &block)
  features.each do |feature|
    start(feature)
  end

  auto_register_paths.each do |(path, path_block)|
    auto_register!(path, set_load_paths: false, load_files: true, &path_block)
  end

  super
end
refresh_boot_files() click to toggle source

This is called when reloading in dev mode

@return [self]

@api private

# File lib/dry/rails/container.rb, line 159
def refresh_boot_files
  booter.boot_files.each do |boot_file|
    load(boot_file)
  end
  self
end
require_path(path) click to toggle source

@api private

# File lib/dry/rails/container.rb, line 149
def require_path(path)
  require_dependency(path)
end