-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
21128f4
commit 72d2929
Showing
5 changed files
with
58 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
require "smart_core/initializer" | ||
|
||
require_relative "resol/version" | ||
require_relative "resol/return_engine" | ||
require_relative "resol/configuration" | ||
require_relative "resol/service" | ||
|
||
module Resol | ||
extend self | ||
|
||
def config | ||
Configuration | ||
end | ||
|
||
def configure | ||
yield config | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,32 @@ | ||
# frozen_string_literal: true | ||
|
||
module Resol | ||
class Configuration | ||
DEFAULT_RETURN_ENGINE = ReturnEngine::Catch | ||
|
||
class << self | ||
def configure | ||
SmartCore::Initializer::Configuration.configure do |c| | ||
self.smartcore_config = c | ||
yield self | ||
self.smartcore_config = nil | ||
end | ||
end | ||
|
||
def return_engine | ||
@return_engine || DEFAULT_RETURN_ENGINE | ||
end | ||
|
||
def return_engine=(engine) | ||
@return_engine = engine | ||
end | ||
|
||
private | ||
|
||
attr_accessor :smartcore_config | ||
|
||
def method_missing(meth, *, &) | ||
# rubocop:disable Style/SafeNavigation | ||
if smartcore_config && smartcore_config.respond_to?(meth) | ||
# rubocop:enable Style/SafeNavigation | ||
smartcore_config.__send__(meth, *, &) | ||
else | ||
super | ||
end | ||
end | ||
|
||
def respond_to_missing?(meth, include_private) | ||
smartcore_config.respond_to?(meth, include_private) | ||
end | ||
module Configuration | ||
extend self | ||
|
||
DEFAULTS = { return_engine: Resol::ReturnEngine::Catch }.freeze | ||
|
||
DEFAULTS.each_key do |attr_name| | ||
define_method(attr_name) { values[attr_name] } | ||
define_method(:"#{attr_name}=") { |value| values[attr_name] = value } | ||
end | ||
|
||
def smart_config | ||
return nil if smart_not_loaded? | ||
|
||
SmartCore::Initializer::Configuration.config | ||
end | ||
|
||
def to_h = values.dup | ||
|
||
private | ||
|
||
def smart_not_loaded? | ||
!defined?(SmartCore::Initializer::Configuration) | ||
end | ||
|
||
def values | ||
@values ||= DEFAULTS.dup | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe Resol::Configuration do | ||
around do |example| | ||
example.call | ||
let(:cfg_values) { described_class.instance_variable_get(:@values) } | ||
|
||
described_class.configure do |c| | ||
c.auto_cast = true | ||
c.return_engine = described_class::DEFAULT_RETURN_ENGINE | ||
end | ||
end | ||
it "properly configures" do | ||
expect(described_class.return_engine).to eq(described_class::DEFAULTS[:return_engine]) | ||
expect(described_class.smart_config).to eq(SmartCore::Initializer::Configuration.config) | ||
|
||
it "delegates configuration" do | ||
described_class.configure do |c| | ||
c.auto_cast = false | ||
c.return_engine = Resol::ReturnEngine::Return | ||
end | ||
described_class.return_engine = "kek" | ||
|
||
expect(SmartCore::Initializer::Configuration.config[:auto_cast]).to eq(false) | ||
expect(described_class.return_engine).to eq(Resol::ReturnEngine::Return) | ||
expect(described_class.return_engine).to eq("kek") | ||
expect(described_class.to_h.equal?(cfg_values)).to eq(false) | ||
end | ||
|
||
context "when undefined method is called" do | ||
let(:called_block) do | ||
proc do | ||
described_class.configure do |c| | ||
c.not_exist = true | ||
end | ||
end | ||
end | ||
|
||
it "raises error" do | ||
expect(&called_block).to raise_error(NoMethodError) | ||
specify do | ||
expect { described_class.kekpek }.to raise_error(NoMethodError) | ||
end | ||
end | ||
|
||
context "with undefined method" do | ||
it "respond_to? returns false" do | ||
expect(described_class.respond_to?(:not_exist)).to eq(false) | ||
end | ||
context "when smartcore not loaded" do | ||
before { allow(described_class).to receive(:smart_not_loaded?).and_return(true) } | ||
|
||
specify { expect(described_class.smart_config).to eq(nil) } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters