Skip to content

Commit

Permalink
[wip] turn Plugins module into global plugins container
Browse files Browse the repository at this point in the history
  • Loading branch information
solnic committed Dec 26, 2021
1 parent 93b3b76 commit 80a0082
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 113 deletions.
2 changes: 1 addition & 1 deletion lib/rom/commands/class_interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def create_class(type: self, meta: {}, rel_meta: {}, plugins: {}, **, &block)
#
# @api public
def use(plugin, **options)
ROM.plugin_registry[:command].fetch(plugin, adapter).apply_to(self, **options)
ROM.plugins[:command].fetch(plugin, adapter).apply_to(self, **options)
end

# Set before-execute hooks
Expand Down
19 changes: 19 additions & 0 deletions lib/rom/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,25 @@ def components(&block)
end
end

# Global plugin setup
#
# @example
# ROM.plugins do
# register :publisher, Plugin::Publisher, type: :command
# end
#
# @api public
def plugins(*args, &block)
if defined?(@_plugins)
@_plugins.dsl(*args, &block) if block
@_plugins
else
require_relative "plugins"
@_plugins = Plugins
plugins(*args, &block)
end
end

# Register core component handlers
components do
register :gateway, Components::Gateway
Expand Down
24 changes: 4 additions & 20 deletions lib/rom/global.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

require "rom/components"
require "rom/plugins/container"
require "rom/plugins/dsl"

module ROM
# Globally accessible public interface exposed via ROM module
Expand All @@ -16,7 +14,6 @@ def self.extended(rom)
super

rom.instance_variable_set("@adapters", {})
rom.instance_variable_set("@plugin_registry", Plugins::Container.new)
end

# An internal adapter identifier => adapter module map used by setup
Expand All @@ -33,12 +30,11 @@ def self.extended(rom)
# @api private
attr_reader :handlers

# An internal plugin registry
#
# @return [Plugins]
#
# @api private
attr_reader :plugin_registry
# @deprecated
def plugin_registry
plugins
end

# @api public
def runtime(*args, &block)
Expand All @@ -50,18 +46,6 @@ def runtime(*args, &block)
end.finalize
end

# Global plugin setup DSL
#
# @example
# ROM.plugins do
# register :publisher, Plugin::Publisher, type: :command
# end
#
# @api public
def plugins(*args, &block)
Plugins::DSL.new(plugin_registry, *args, &block)
end

# Register adapter namespace under a specified identifier
#
# @param [Symbol] identifier
Expand Down
93 changes: 93 additions & 0 deletions lib/rom/plugins.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,100 @@
# frozen_string_literal: true

require "dry/container"

require "rom/constants"
require "rom/plugin"

require "rom/plugins/dsl"
require "rom/plugins/relation/registry_reader"
require "rom/plugins/relation/instrumentation"
require "rom/plugins/command/schema"
require "rom/plugins/command/timestamps"
require "rom/plugins/schema/timestamps"

module ROM
# Registry of all known plugins
#
# @api public
module Plugins
extend Dry::Container::Mixin
extend Enumerable

module_function

def dsl(*args, &block)
Plugins::DSL.new(self, *args, &block)
end

# @api private
def register(name, type:, **options)
Plugin.new(name: name, type: type, **options).tap do |plugin|
super(plugin.key, plugin)
end
end

# @api private
def each
keys.each { |key| yield(self[key]) }
end

# @api private
def resolve(key)
super
rescue Dry::Container::Error
raise ROM::UnknownPluginError, "+#{key}+ plugin was not found"
end

# TODO: move to rom/compat
# @api private
class Resolver
attr_reader :container, :type, :_adapter

# @api private
def initialize(container, type:, adapter: nil)
@container = container
@type = type
@_adapter = adapter
end

# @api private
def adapter(name)
self.class.new(container, type: type, adapter: name)
end

# @api private
def fetch(name, adapter = nil)
if adapter
key = [adapter, type, name].compact.join(".")

if container.key?(key)
container.resolve(key)
else
fetch(name)
end
else
if _adapter && key?(name)
fetch(name, _adapter)
else
key = "#{type}.#{name}"
container.resolve(key)
end
end
end

# @api private
def key?(name)
container.key?([_adapter, type, name].compact.join("."))
end
end

# @api private
def [](key)
if key?(key)
super
else
Resolver.new(self, type: key)
end
end
end
end
89 changes: 0 additions & 89 deletions lib/rom/plugins/container.rb

This file was deleted.

6 changes: 3 additions & 3 deletions spec/suite/legacy/unit/rom/plugin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ def build_type(*)
end

around do |example|
keys = ROM.plugin_registry.keys
keys = ROM.plugins.keys
example.run
(ROM.plugin_registry.keys - keys).each { |key| ROM.plugin_registry._container.delete(key) }
(ROM.plugins.keys - keys).each { |key| ROM.plugins._container.delete(key) }
end

it "makes configuration plugins available" do
expect(ROM.plugin_registry[:configuration].fetch(:registration).mod)
expect(ROM.plugins[:configuration].fetch(:registration).mod)
.to eq Test::ConfigurationPlugin
end

Expand Down

0 comments on commit 80a0082

Please sign in to comment.