diff --git a/lib/resol/plugins.rb b/lib/resol/plugins.rb index 936a0de..15e606c 100644 --- a/lib/resol/plugins.rb +++ b/lib/resol/plugins.rb @@ -6,6 +6,10 @@ module Resol module Plugins PLUGINS_PATH = Pathname("resol/plugins") class Manager + def self.resolve_module(module_name) + Plugins.const_get(module_name) + end + def initialize self.allowed_classes = resolve_allowed_classes self.plugins = [] @@ -14,7 +18,7 @@ def initialize def plugin(caller_class, plugin_name) plugin_name = plugin_name.to_s - return if allowed_classes.exclude?(caller_class) + return unless allowed_classes.include?(caller_class) return if plugins.include?(plugin_name) plugin_module = find_plugin_module(plugin_name) @@ -39,15 +43,11 @@ def resolve_allowed_classes def find_plugin_module(plugin_name) require PLUGINS_PATH.join(plugin_name) - resolve_module(classify_plugin_name(plugin_name)) + self.class.resolve_module(classify_plugin_name(plugin_name)) rescue LoadError, NameError => e raise ArgumentError, "Failed to load plugin '#{plugin_name}': #{e.message}" end - def resolve_module(module_name) - Plugins.const_get(module_name) - end - def classify_plugin_name(string) string.split(/_|-/).map!(&:capitalize).join end diff --git a/lib/resol/result.rb b/lib/resol/result.rb index 0d1facd..d5d65cf 100644 --- a/lib/resol/result.rb +++ b/lib/resol/result.rb @@ -1,4 +1,4 @@ -# frozen_string_literal: true +1# frozen_string_literal: true module Resol class UnwrapError < StandardError; end diff --git a/spec/initializers_spec.rb b/spec/initializers_spec.rb deleted file mode 100644 index 84a3a8c..0000000 --- a/spec/initializers_spec.rb +++ /dev/null @@ -1,83 +0,0 @@ -# frozen_string_literal: true - -RSpec.describe Resol::Initializers do - def apply_initializer(forced_service_class = nil) - described_class.apply!(forced_service_class || service_class, initializer_name) - end - - before { stub_const("InitializerTestClass", service_class) } - - let(:service_class) do - Class.new(Resol::Service) do - def call - success! - end - end - end - - let(:initializer_name) { :dry } - let(:dry_modules) do - ["Dry::Initializer::Mixin::Root", start_with("Dry::Initializer::Mixin::Local")] - end - - it "properly extend service class with initializer" do - apply_initializer - expect(service_class.ancestors.map(&:to_s)).to include(*dry_modules) - end - - context "with unknown initializer lib" do - let(:initializer_name) { :kek } - - specify do - expect { apply_initializer }.to raise_error(ArgumentError, "unknown initializer kek") - end - end - - context "with already prepared parent" do - before { stub_const("SecondChildService", second_child_service) } - - let(:service_class) do - Class.new(ReturnEngineService) do - def call - success! - end - end - end - - let(:second_child_service) do - Class.new(InitializerTestClass) - end - - let(:error_message) do - "ReturnEngineService or his superclasses already used initialize lib" - end - - specify do - expect { apply_initializer(SecondChildService) }.to raise_error(ArgumentError, error_message) - end - end - - context "with manually extended service" do - before { stub_const("SecondChildService", second_child_service) } - - let(:service_class) do - Class.new(Resol::Service) do - extend Dry::Initializer - - def call - success! - end - end - end - - let(:second_child_service) do - Class.new(InitializerTestClass) - end - - let(:error_message) { "use ::use_initializer! method on desired service class" } - - specify do - expect { apply_initializer(SecondChildService) }.to raise_error(ArgumentError, error_message) - end - end -end diff --git a/spec/service_spec.rb b/spec/service_spec.rb index 314b665..f248b85 100644 --- a/spec/service_spec.rb +++ b/spec/service_spec.rb @@ -314,10 +314,11 @@ def call context "when install plugin on the child service class" do let(:child_service) { Class.new(Resol::Service) } - let(:error_message) { "can load plugins only on base Resol::Service" } + it "just skips installation" do + child_service.plugin(:dummy) + manager = child_service.send(:manager) - it "raises error" do - expect { child_service.plugin(:dump) }.to raise_error(ArgumentError, error_message) + expect(manager.send(:plugins)).to eq([]) end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a99c4f4..778689e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -32,6 +32,7 @@ require "resol/plugins/dummy" Resol::DependencyContainer.enable_stubs! +Resol.config.send(:data=, { classes_allowed_to_patch: ["Resol::Service", "ReturnEngineService"] }) class SmartService < Resol::Service inject_initializer! :smartcore_injector @@ -52,10 +53,4 @@ class ReturnEngineService < Resol::Service config.order = :random Kernel.srand config.seed - - config.around do |ex| - applied_classes = Resol::Initializers.send(:applied_classes).dup - ex.call - Resol::Initializers.send(:applied_classes=, applied_classes) - end end