Skip to content

Commit

Permalink
Finish refactoring and fixing after refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
AnotherRegularDude committed Dec 15, 2024
1 parent 31dd7d6 commit 70cb439
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 79 deletions.
9 changes: 9 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ PATH
remote: .
specs:
resol (1.0.0)
dry-configurable
dry-container

GEM
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ 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
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
Expand Down
9 changes: 5 additions & 4 deletions lib/resol.rb
Original file line number Diff line number Diff line change
@@ -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(...)
Expand Down
19 changes: 0 additions & 19 deletions lib/resol/configuration.rb

This file was deleted.

4 changes: 2 additions & 2 deletions lib/resol/injector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -20,7 +20,7 @@ def inject!(service_class)
attr_accessor :proc_register

def error!(msg)
raise msg
raise msg
end
end
end
1 change: 1 addition & 0 deletions resol.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 0 additions & 27 deletions spec/configuration_spec.rb

This file was deleted.

24 changes: 14 additions & 10 deletions spec/manager_spec.rb
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -12,41 +17,40 @@
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
end
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
Expand Down
14 changes: 0 additions & 14 deletions spec/resol_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 12 additions & 0 deletions spec/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 8 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
end

require "dry/container/stub"
require "dry/configurable/test_interface"
require "resol"
require "pry"

Expand All @@ -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
Expand All @@ -53,4 +58,6 @@ class ReturnEngineService < Resol::Service

config.order = :random
Kernel.srand config.seed

config.before { Resol.reset_config }
end

0 comments on commit 70cb439

Please sign in to comment.