-
-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Zeitwerk for loading components (#634)
[changelog] added: - "Backward-compatibility extension that you can load via `require "rom/compat"` (via #634 refs #607) (@solnic)" changed: - "[BREAKING] `Setup#auto_registration` was renamed to `Setup#auto_register`. You can restore the original method via rom/compat extension (via #634 refs #607) (@solnic)" - "[internal] auto registration is now powered by Zeitwerk (via #634 refs #607) (@solnic)"
- Loading branch information
Showing
57 changed files
with
586 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "compat/setup" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "auto_registration" | ||
|
||
module ROM | ||
# Setup objects collect component classes during setup/finalization process | ||
# | ||
# @api public | ||
class Setup | ||
# Enable auto-registration for a given setup object | ||
# | ||
# @param [String, Pathname] directory The root path to components | ||
# @param [Hash] options | ||
# @option options [Boolean, String] :namespace Toggle root namespace | ||
# or provide a custom namespace name | ||
# | ||
# @return [Setup] | ||
# | ||
# @deprecated | ||
# | ||
# @api public | ||
def auto_registration(directory, **options) | ||
auto_registration = AutoRegistration.new(directory, inflector: inflector, **options) | ||
auto_registration.relations.map { |r| register_relation(r) } | ||
auto_registration.commands.map { |r| register_command(r) } | ||
auto_registration.mappers.map { |r| register_mapper(r) } | ||
self | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
# frozen_string_literal: true | ||
|
||
require "pathname" | ||
require "zeitwerk" | ||
|
||
require "rom/support/inflector" | ||
|
||
require "rom/types" | ||
require "rom/initializer" | ||
require "rom/loader" | ||
|
||
module ROM | ||
# AutoRegistration is used to load component files automatically from the provided directory path | ||
# | ||
# @api public | ||
class Loader | ||
extend Initializer | ||
|
||
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 | ||
|
||
# @!attribute [r] directory | ||
# @return [Pathname] The root path | ||
param :root_directory, type: PathnameType | ||
|
||
# @!attribute [r] namespace | ||
# @return [Boolean,String] | ||
# The name of the top level namespace or true/false which | ||
# enables/disables default top level namespace inferred from the dir name | ||
option :namespace, type: NamespaceType, default: -> { true } | ||
|
||
# @!attribute [r] component_dirs | ||
# @return [Hash] component => dir-name map | ||
option :component_dirs, type: ComponentDirs, default: -> { DEFAULT_MAPPING } | ||
|
||
# @!attribute [r] inflector | ||
# @return [Dry::Inflector] String inflector | ||
# @api private | ||
option :inflector, type: InflectorType, default: -> { Inflector } | ||
|
||
# @!attribute [r] loaded_constants | ||
# @return [Dry::Inflector] String inflector | ||
# @api private | ||
option :inflector, type: InflectorType, default: -> { Inflector } | ||
|
||
option :loaded_constants, default: -> { {relations: [], commands: [], mappers: []} } | ||
|
||
# Load components | ||
# | ||
# @api private | ||
def constants(component_type) | ||
unless @loaded | ||
setup and backend.eager_load | ||
@loaded = true | ||
end | ||
|
||
loaded_constants.fetch(component_type) | ||
end | ||
|
||
# Load relation files | ||
# | ||
# @api private | ||
def relations | ||
constants(__method__) | ||
end | ||
|
||
# Load command files | ||
# | ||
# @api private | ||
def commands | ||
constants(__method__) | ||
end | ||
|
||
# Load mapper files | ||
# | ||
# @api private | ||
def mappers | ||
constants(__method__) | ||
end | ||
|
||
private | ||
|
||
# @api private | ||
def backend | ||
@backend ||= Zeitwerk::Loader.new | ||
end | ||
|
||
# @api private | ||
# rubocop:disable Metrics/AbcSize | ||
def setup | ||
backend.inflector = inflector | ||
|
||
case namespace | ||
when true | ||
backend.push_dir(root_directory.join("..").realpath) | ||
|
||
component_dirs.each_value do |dir| | ||
backend.collapse(root_directory.join(dir).join("**/*")) | ||
end | ||
when false | ||
backend.push_dir(root_directory) | ||
else | ||
backend.push_dir(root_directory, namespace: namespace) | ||
end | ||
|
||
excluded_dirs.each do |dir| | ||
backend.ignore(dir) | ||
end | ||
|
||
backend.on_load do |_, const, path| | ||
if (type = path_to_component_type(path)) | ||
loaded_constants[type] << const | ||
end | ||
end | ||
|
||
backend.setup | ||
end | ||
# rubocop:enable Metrics/AbcSize | ||
|
||
# @api private | ||
def path_to_component_type(path) | ||
return unless File.file?(path) | ||
|
||
component_dirs | ||
.detect { |_, dir| | ||
path.start_with?(root_directory.join(dir).to_s) | ||
} | ||
&.first | ||
end | ||
|
||
# @api private | ||
def excluded_dirs | ||
root_directory | ||
.children | ||
.select(&:directory?) | ||
.reject { |dir| component_dirs.values.include?(dir.basename.to_s) } | ||
.map { |name| root_directory.join(name) } | ||
end | ||
end | ||
end |
Oops, something went wrong.