From 70cb4394f6d81ff85836341597b4df7ebc9ff491 Mon Sep 17 00:00:00 2001 From: AnotherRegularDude Date: Mon, 16 Dec 2024 02:11:05 +0300 Subject: [PATCH] Finish refactoring and fixing after refactoring --- Gemfile.lock | 9 +++++++++ README.md | 4 ++-- lib/resol.rb | 9 +++++---- lib/resol/configuration.rb | 19 ------------------- lib/resol/injector.rb | 4 ++-- resol.gemspec | 1 + spec/configuration_spec.rb | 27 --------------------------- spec/manager_spec.rb | 24 ++++++++++++++---------- spec/resol_spec.rb | 14 -------------- spec/service_spec.rb | 12 ++++++++++++ spec/spec_helper.rb | 9 ++++++++- 11 files changed, 53 insertions(+), 79 deletions(-) delete mode 100644 lib/resol/configuration.rb delete mode 100644 spec/configuration_spec.rb diff --git a/Gemfile.lock b/Gemfile.lock index bf40452..6ebd048 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,6 +2,7 @@ PATH remote: . specs: resol (1.0.0) + dry-configurable dry-container GEM @@ -37,8 +38,15 @@ GEM diff-lcs (1.5.1) docile (1.4.1) drb (2.2.1) + dry-configurable (1.2.0) + dry-core (~> 1.0, < 2) + zeitwerk (~> 2.6) dry-container (0.11.0) concurrent-ruby (~> 1.0) + dry-core (1.0.2) + concurrent-ruby (~> 1.0) + logger + zeitwerk (~> 2.6) dry-inflector (1.1.0) dry-initializer (3.1.1) i18n (1.14.6) @@ -134,6 +142,7 @@ GEM umbrellio-sequel-plugins (0.17.0) sequel unicode-display_width (2.6.0) + zeitwerk (2.7.1) PLATFORMS arm64-darwin-21 diff --git a/README.md b/README.md index 98ad809..472eebe 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ You can use both providers for a different services: # Types is a namespace for all types, defined by smart_types. class FirstService < Resol::Service - use_initializer! :smartcore + inject_initializer :smartcore param :first, Types::String param :second, Types::Integer @@ -76,7 +76,7 @@ end # Types is a namespace for all types, defined by dry-types. class SecondService < Resol::Service - use_initializer! :dry + inject_initializer :dry param :first, Types::Strict::String param :second, Types::Strict::Integer diff --git a/lib/resol.rb b/lib/resol.rb index 3b0e9bb..bba380e 100644 --- a/lib/resol.rb +++ b/lib/resol.rb @@ -1,21 +1,22 @@ # frozen_string_literal: true +require "dry-configurable" require "dry-container" require_relative "resol/version" -require_relative "resol/configuration" require_relative "resol/injector" require_relative "resol/service" require_relative "resol/plugins" + require_relative "resol/dependency_container" module Resol extend self - def config - @config ||= Configuration.new - end + extend Dry::Configurable + + setting :classes_allowed_to_patch, default: ["Resol::Service"] # rubocop:disable Naming/MethodName def Success(...) diff --git a/lib/resol/configuration.rb b/lib/resol/configuration.rb deleted file mode 100644 index a05a63c..0000000 --- a/lib/resol/configuration.rb +++ /dev/null @@ -1,19 +0,0 @@ -# frozen_string_literal: true - -module Resol - class Configuration - DEFAULT_CONFIG_VALUES = { classes_allowed_to_patch: ["Resol::Service"] }.freeze - - def initialize - self.data = DEFAULT_CONFIG_VALUES.dup - end - - DEFAULT_CONFIG_VALUES.each_key do |setting_name| - define_method(setting_name) { data.fetch(setting_name) } - end - - private - - attr_accessor :data - end -end diff --git a/lib/resol/injector.rb b/lib/resol/injector.rb index ebcaa5f..b48589a 100644 --- a/lib/resol/injector.rb +++ b/lib/resol/injector.rb @@ -9,7 +9,7 @@ def initialize(proc_register) end def inject!(service_class) - error!("parent or this class already injected") if service_class.is_a?(InjectMarker) + error!("parent or this class already injected") if service_class.include?(InjectMarker) service_class.instance_eval(&proc_register) service_class.include(InjectMarker) @@ -20,7 +20,7 @@ def inject!(service_class) attr_accessor :proc_register def error!(msg) - raise msg + raise msg end end end diff --git a/resol.gemspec b/resol.gemspec index b2957ed..ad56eef 100644 --- a/resol.gemspec +++ b/resol.gemspec @@ -17,5 +17,6 @@ Gem::Specification.new do |spec| spec.files = `git ls-files -z`.split("\x0").reject { |f| f.include?("spec") } spec.require_paths = ["lib"] + spec.add_dependency "dry-configurable" spec.add_dependency "dry-container" end diff --git a/spec/configuration_spec.rb b/spec/configuration_spec.rb deleted file mode 100644 index 020ff9f..0000000 --- a/spec/configuration_spec.rb +++ /dev/null @@ -1,27 +0,0 @@ -# frozen_string_literal: true - -RSpec.describe Resol::Configuration do - before { allow(described_class).to receive(:smart_not_loaded?).and_return(const_not_loaded?) } - - let(:const_not_loaded?) { true } - - it "#smart_config returns nil" do - expect(described_class.smart_config).to eq(nil) - end - - context "with loaded const" do - let(:const_not_loaded?) { false } - - it "returns config" do - expect(described_class.smart_config).to eq(SmartCore::Initializer::Configuration.config) - end - end - - context "with original method" do - before { allow(described_class).to receive(:smart_not_loaded?).and_call_original } - - it "returns smartcore config" do - expect(described_class.smart_config).to eq(SmartCore::Initializer::Configuration.config) - end - end -end diff --git a/spec/manager_spec.rb b/spec/manager_spec.rb index f56b11e..797f489 100644 --- a/spec/manager_spec.rb +++ b/spec/manager_spec.rb @@ -1,7 +1,12 @@ # frozen_string_literal: true RSpec.describe Resol::Plugins::Manager do - let(:manager) { described_class.new(service_double) } + before do + Resol.config.classes_allowed_to_patch = %w[DummyService Resol::Service ReturnEngineService] + end + before { stub_const("DummyService", service_double) } + + let(:manager) { described_class.new } let(:service_double) do class_double( @@ -12,30 +17,29 @@ let(:singleton_double) { double(prepend: true) } it "skips all prepends" do - manager.plugin(:dummy) + manager.plugin(DummyService, :dummy) expect(service_double).not_to receive(:prepend) expect(singleton_double).not_to receive(:prepend) end context "when uses same plugin few times" do - before { allow(manager).to receive(:resolve_module).and_return(Resol::Plugins::Dummy) } - - before { manager.plugin(:dummy) } + before { allow(described_class).to receive(:resolve_module).and_return(Resol::Plugins::Dummy) } let(:manager_plugins) { manager.instance_variable_get(:@plugins) } it "doesn't load plugin second time" do - manager.plugin(:dummy) + manager.plugin(DummyService, :dummy) + manager.plugin(DummyService, :dummy) - expect(manager).to have_received(:resolve_module).once + expect(described_class).to have_received(:resolve_module).once expect(manager_plugins).to eq(["dummy"]) end end context "when can't require plugin" do specify do - expect { manager.plugin(:not_existed_plugin) }.to raise_error do |error| + expect { manager.plugin(DummyService, :not_existed_plugin) }.to raise_error do |error| expect(error).to be_an_instance_of(ArgumentError) expect(error.message).to include("Failed to load plugin 'not_existed_plugin': ") end @@ -43,10 +47,10 @@ end context "when can't resolve module" do - before { allow(manager).to receive(:resolve_module).and_raise(NameError, "msg") } + before { allow(described_class).to receive(:resolve_module).and_raise(NameError, "msg") } specify do - expect { manager.plugin(:dummy) }.to raise_error do |error| + expect { manager.plugin(DummyService, :dummy) }.to raise_error do |error| expect(error).to be_an_instance_of(ArgumentError) expect(error.message).to include("Failed to load plugin 'dummy': msg") end diff --git a/spec/resol_spec.rb b/spec/resol_spec.rb index d419f6d..46f84fe 100644 --- a/spec/resol_spec.rb +++ b/spec/resol_spec.rb @@ -4,18 +4,4 @@ it "has a version number" do expect(Resol::VERSION).not_to be nil end - - describe "#config" do - specify do - expect(described_class.config).to eq(Resol::Configuration) - end - end - - describe "#configure" do - specify do - Resol.configure do |config| - expect(config.smart_config).to eq(SmartCore::Initializer::Configuration.config) - end - end - end end diff --git a/spec/service_spec.rb b/spec/service_spec.rb index f248b85..1d102b6 100644 --- a/spec/service_spec.rb +++ b/spec/service_spec.rb @@ -330,4 +330,16 @@ def call expect(first_manager).to eq(second_manager) end end + + context "when inherited from already injected service" do + let(:child_service) { Class.new(SmartService) } + let(:injecting_proc) { proc { inject_initializer!(:dry_injector) } } + + it "tries to inject initializer" do + expect { child_service.class_eval(&injecting_proc) }.to raise_error do |error| + expect(error).to be_instance_of(RuntimeError) + expect(error.message).to eq("parent or this class already injected") + end + end + end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 778689e..5472d2d 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -23,6 +23,7 @@ end require "dry/container/stub" +require "dry/configurable/test_interface" require "resol" require "pry" @@ -31,8 +32,12 @@ require "resol/plugins/dummy" +module Resol + enable_test_interface +end + Resol::DependencyContainer.enable_stubs! -Resol.config.send(:data=, { classes_allowed_to_patch: ["Resol::Service", "ReturnEngineService"] }) +Resol.config.classes_allowed_to_patch = %w[Resol::Service ReturnEngineService] class SmartService < Resol::Service inject_initializer! :smartcore_injector @@ -53,4 +58,6 @@ class ReturnEngineService < Resol::Service config.order = :random Kernel.srand config.seed + + config.before { Resol.reset_config } end