Skip to content

Commit 72d2929

Browse files
Write specs
1 parent 21128f4 commit 72d2929

File tree

5 files changed

+58
-68
lines changed

5 files changed

+58
-68
lines changed

lib/resol.rb

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
# frozen_string_literal: true
22

3-
require "smart_core/initializer"
4-
53
require_relative "resol/version"
64
require_relative "resol/return_engine"
75
require_relative "resol/configuration"
86
require_relative "resol/service"
97

108
module Resol
9+
extend self
10+
11+
def config
12+
Configuration
13+
end
14+
15+
def configure
16+
yield config
17+
end
1118
end

lib/resol/configuration.rb

+26-37
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,32 @@
11
# frozen_string_literal: true
22

33
module Resol
4-
class Configuration
5-
DEFAULT_RETURN_ENGINE = ReturnEngine::Catch
6-
7-
class << self
8-
def configure
9-
SmartCore::Initializer::Configuration.configure do |c|
10-
self.smartcore_config = c
11-
yield self
12-
self.smartcore_config = nil
13-
end
14-
end
15-
16-
def return_engine
17-
@return_engine || DEFAULT_RETURN_ENGINE
18-
end
19-
20-
def return_engine=(engine)
21-
@return_engine = engine
22-
end
23-
24-
private
25-
26-
attr_accessor :smartcore_config
27-
28-
def method_missing(meth, *, &)
29-
# rubocop:disable Style/SafeNavigation
30-
if smartcore_config && smartcore_config.respond_to?(meth)
31-
# rubocop:enable Style/SafeNavigation
32-
smartcore_config.__send__(meth, *, &)
33-
else
34-
super
35-
end
36-
end
37-
38-
def respond_to_missing?(meth, include_private)
39-
smartcore_config.respond_to?(meth, include_private)
40-
end
4+
module Configuration
5+
extend self
6+
7+
DEFAULTS = { return_engine: Resol::ReturnEngine::Catch }.freeze
8+
9+
DEFAULTS.each_key do |attr_name|
10+
define_method(attr_name) { values[attr_name] }
11+
define_method(:"#{attr_name}=") { |value| values[attr_name] = value }
12+
end
13+
14+
def smart_config
15+
return nil if smart_not_loaded?
16+
17+
SmartCore::Initializer::Configuration.config
18+
end
19+
20+
def to_h = values.dup
21+
22+
private
23+
24+
def smart_not_loaded?
25+
!defined?(SmartCore::Initializer::Configuration)
26+
end
27+
28+
def values
29+
@values ||= DEFAULTS.dup
4130
end
4231
end
4332
end

resol.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ Gem::Specification.new do |spec|
1717
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.include?("spec") }
1818
spec.require_paths = ["lib"]
1919

20-
spec.add_dependency "dry-initializer", "~> 3.1"
20+
spec.add_dependency "dry-initializer", "~> 3.1"
2121
spec.add_dependency "smart_initializer", "~> 0.7"
2222
end

spec/configuration_spec.rb

+13-28
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,27 @@
11
# frozen_string_literal: true
22

33
RSpec.describe Resol::Configuration do
4-
around do |example|
5-
example.call
4+
let(:cfg_values) { described_class.instance_variable_get(:@values) }
65

7-
described_class.configure do |c|
8-
c.auto_cast = true
9-
c.return_engine = described_class::DEFAULT_RETURN_ENGINE
10-
end
11-
end
6+
it "properly configures" do
7+
expect(described_class.return_engine).to eq(described_class::DEFAULTS[:return_engine])
8+
expect(described_class.smart_config).to eq(SmartCore::Initializer::Configuration.config)
129

13-
it "delegates configuration" do
14-
described_class.configure do |c|
15-
c.auto_cast = false
16-
c.return_engine = Resol::ReturnEngine::Return
17-
end
10+
described_class.return_engine = "kek"
1811

19-
expect(SmartCore::Initializer::Configuration.config[:auto_cast]).to eq(false)
20-
expect(described_class.return_engine).to eq(Resol::ReturnEngine::Return)
12+
expect(described_class.return_engine).to eq("kek")
13+
expect(described_class.to_h.equal?(cfg_values)).to eq(false)
2114
end
2215

2316
context "when undefined method is called" do
24-
let(:called_block) do
25-
proc do
26-
described_class.configure do |c|
27-
c.not_exist = true
28-
end
29-
end
30-
end
31-
32-
it "raises error" do
33-
expect(&called_block).to raise_error(NoMethodError)
17+
specify do
18+
expect { described_class.kekpek }.to raise_error(NoMethodError)
3419
end
3520
end
3621

37-
context "with undefined method" do
38-
it "respond_to? returns false" do
39-
expect(described_class.respond_to?(:not_exist)).to eq(false)
40-
end
22+
context "when smartcore not loaded" do
23+
before { allow(described_class).to receive(:smart_not_loaded?).and_return(true) }
24+
25+
specify { expect(described_class.smart_config).to eq(nil) }
4126
end
4227
end

spec/spec_helper.rb

+9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
require "resol"
2626
require "pry"
2727

28+
require "smart_core/initializer"
29+
require "dry/initializer"
30+
2831
class SmartService < Resol::Service
2932
use_initializer! :smartcore
3033
end
@@ -36,4 +39,10 @@ class SmartService < Resol::Service
3639

3740
config.order = :random
3841
Kernel.srand config.seed
42+
43+
config.around do |ex|
44+
old_settings = Resol::Configuration.to_h
45+
ex.call
46+
Resol::Configuration.instance_variable_set(:@values, old_settings)
47+
end
3948
end

0 commit comments

Comments
 (0)