Class: ROM::Loader

Inherits:
Object
  • Object
show all
Extended by:
Initializer
Defined in:
lib/rom/loader.rb

Overview

AutoRegistration is used to load component files automatically from the provided directory path

Constant Summary collapse

NamespaceType =
Types::Strict::Bool | Types.Instance(Module)
PathnameType =
Types.Constructor(Pathname, &Kernel.method(:Pathname))
InflectorType =
Types.Interface(:camelize)
ComponentDirs =
Types::Strict::Hash.constructor { |hash| hash.transform_values(&:to_s) }
DEFAULT_MAPPING =
{
  relations: "relations",
  mappers: "mappers",
  commands: "commands"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#auto_loadBoolean (readonly)

Returns Whether files should be auto-loadable.

Returns:

  • (Boolean)

    Whether files should be auto-loadable



40
# File 'lib/rom/loader.rb', line 40

option :auto_load, type: Types::Strict::Bool, default: -> { false }

#component_dirsHash (readonly)

Returns component => dir-name map.

Returns:

  • (Hash)

    component => dir-name map



50
# File 'lib/rom/loader.rb', line 50

option :component_dirs, type: ComponentDirs, default: -> { DEFAULT_MAPPING }

#componentsROM::Components (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.

Returns:



60
# File 'lib/rom/loader.rb', line 60

option :components

#directoryPathname (readonly)

Returns The root path.

Returns:

  • (Pathname)

    The root path



35
# File 'lib/rom/loader.rb', line 35

param :root_directory, type: PathnameType.optional

#inflectorDry::Inflector (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.

Returns String inflector.

Returns:

  • (Dry::Inflector)

    String inflector



55
# File 'lib/rom/loader.rb', line 55

option :inflector, type: InflectorType, default: -> { Inflector }

#namespaceBoolean, String (readonly)

Returns The name of the top level namespace or true/false which enables/disables default top level namespace inferred from the dir name.

Returns:

  • (Boolean, String)

    The name of the top level namespace or true/false which enables/disables default top level namespace inferred from the dir name



46
# File 'lib/rom/loader.rb', line 46

option :namespace, type: NamespaceType, default: -> { true }

Instance Method Details

#auto_load_component_file(type, key) ⇒ 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.



76
77
78
79
80
81
82
83
84
# File 'lib/rom/loader.rb', line 76

def auto_load_component_file(type, key)
  return unless component_dirs.include?(type)

  const_parts = key.split(".").map { |name| inflector.camelize(name) }
  const_parts.unshift(namespace_const_name) if namespace_const_name
  const_name = const_parts.join("::")

  inflector.constantize(const_name)
end

#callObject

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.

Load components



65
66
67
68
69
70
71
72
73
# File 'lib/rom/loader.rb', line 65

def call
  return if @loaded || root_directory.nil?

  setup

  backend.eager_load unless auto_load

  @loaded = true
end