Use dry/configurable if available
This commit is contained in:
parent
e110a190b9
commit
8568600ad4
|
@ -26,9 +26,9 @@ jobs:
|
||||||
fail-fast: false
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
ruby:
|
ruby:
|
||||||
- "3.1"
|
- "3.1"
|
||||||
- "3.0"
|
- "3.0"
|
||||||
- "2.7"
|
- "2.7"
|
||||||
include:
|
include:
|
||||||
- ruby: "3.1"
|
- ruby: "3.1"
|
||||||
coverage: "true"
|
coverage: "true"
|
||||||
|
@ -47,6 +47,8 @@ jobs:
|
||||||
bundler-cache: true
|
bundler-cache: true
|
||||||
- name: Run all tests
|
- name: Run all tests
|
||||||
run: bundle exec rake
|
run: bundle exec rake
|
||||||
|
- name: Run all tests using dry/configurable for config
|
||||||
|
run: bundle exec rake spec:configurable
|
||||||
release:
|
release:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
if: contains(github.ref, 'tags') && github.event_name == 'create'
|
if: contains(github.ref, 'tags') && github.event_name == 'create'
|
||||||
|
|
2
Gemfile
2
Gemfile
|
@ -6,6 +6,8 @@ eval_gemfile "Gemfile.devtools"
|
||||||
|
|
||||||
gemspec
|
gemspec
|
||||||
|
|
||||||
|
gem "dry-configurable"
|
||||||
|
|
||||||
group :tools do
|
group :tools do
|
||||||
gem "pry-byebug", platform: :mri
|
gem "pry-byebug", platform: :mri
|
||||||
end
|
end
|
||||||
|
|
6
Rakefile
6
Rakefile
|
@ -7,3 +7,9 @@ task default: :spec
|
||||||
|
|
||||||
desc "Run all specs in spec directory"
|
desc "Run all specs in spec directory"
|
||||||
RSpec::Core::RakeTask.new(:spec)
|
RSpec::Core::RakeTask.new(:spec)
|
||||||
|
|
||||||
|
desc "Run specs with dry/configurable loaded"
|
||||||
|
task "spec:configurable" do
|
||||||
|
ENV["DRY_CONFIGURABLE"] = "true"
|
||||||
|
Rake::Task["spec"].invoke
|
||||||
|
end
|
||||||
|
|
|
@ -33,12 +33,27 @@ module Dry
|
||||||
|
|
||||||
# @api public
|
# @api public
|
||||||
module Configuration
|
module Configuration
|
||||||
# @api public
|
# Use dry/configurable if it's available
|
||||||
def config
|
if defined?(Configurable)
|
||||||
@config ||= Config.new
|
# @api private
|
||||||
|
def self.extended(klass)
|
||||||
|
super
|
||||||
|
klass.class_eval do
|
||||||
|
extend Dry::Configurable
|
||||||
|
|
||||||
|
setting :namespace_separator, default: Config::DEFAULT_NAMESPACE_SEPARATOR
|
||||||
|
setting :resolver, default: Config::DEFAULT_RESOLVER
|
||||||
|
setting :registry, default: Config::DEFAULT_REGISTRY
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
# @api private
|
||||||
|
def config
|
||||||
|
@config ||= Config.new
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# @api public
|
# @api private
|
||||||
def configure
|
def configure
|
||||||
yield config
|
yield config
|
||||||
end
|
end
|
||||||
|
@ -71,9 +86,7 @@ module Dry
|
||||||
# container.resolve(:item)
|
# container.resolve(:item)
|
||||||
# => 'item'
|
# => 'item'
|
||||||
#
|
#
|
||||||
#
|
|
||||||
# @api public
|
# @api public
|
||||||
# rubocop: disable Metrics/ModuleLength
|
|
||||||
module Mixin
|
module Mixin
|
||||||
# @private
|
# @private
|
||||||
def self.extended(base)
|
def self.extended(base)
|
||||||
|
|
|
@ -5,7 +5,10 @@ RSpec.describe Dry::Container::Mixin do
|
||||||
let(:klass) do
|
let(:klass) do
|
||||||
Class.new { extend Dry::Container::Mixin }
|
Class.new { extend Dry::Container::Mixin }
|
||||||
end
|
end
|
||||||
let(:container) { klass }
|
|
||||||
|
let(:container) do
|
||||||
|
klass
|
||||||
|
end
|
||||||
|
|
||||||
it_behaves_like "a container"
|
it_behaves_like "a container"
|
||||||
end
|
end
|
||||||
|
@ -14,21 +17,71 @@ RSpec.describe Dry::Container::Mixin do
|
||||||
let(:klass) do
|
let(:klass) do
|
||||||
Class.new { include Dry::Container::Mixin }
|
Class.new { include Dry::Container::Mixin }
|
||||||
end
|
end
|
||||||
let(:container) { klass.new }
|
|
||||||
|
let(:container) do
|
||||||
|
klass.new
|
||||||
|
end
|
||||||
|
|
||||||
it_behaves_like "a container"
|
it_behaves_like "a container"
|
||||||
|
|
||||||
context "into a class with a custom .initialize method" do
|
context "into a class with a custom .initialize method" do
|
||||||
let(:klass) do
|
let(:klass) do
|
||||||
Class.new do
|
Class.new do
|
||||||
|
attr_reader :test
|
||||||
|
|
||||||
include Dry::Container::Mixin
|
include Dry::Container::Mixin
|
||||||
def initialize; end
|
|
||||||
|
def initialize
|
||||||
|
@test = true
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "does not fail on missing member variable" do
|
it "does not fail on missing member variable" do
|
||||||
expect { container.register :key, -> {} }.to_not raise_error
|
expect { container.register :key, -> {} }.to_not raise_error
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "doesn't override the original initialize method" do
|
||||||
|
expect(container.test).to be(true)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if defined?(Dry::Configurable)
|
||||||
|
context "using custom settings via Dry::Configurable with a class" do
|
||||||
|
let(:klass) do
|
||||||
|
Class.new do
|
||||||
|
extend Dry::Container::Mixin
|
||||||
|
|
||||||
|
setting :root, default: "/tmp"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
let(:container) do
|
||||||
|
klass
|
||||||
|
end
|
||||||
|
|
||||||
|
it "exposes custom config" do
|
||||||
|
expect(container.config.root).to eql("/tmp")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "using custom settings via Dry::Configurable with an object" do
|
||||||
|
let(:klass) do
|
||||||
|
Class.new do
|
||||||
|
include Dry::Container::Mixin
|
||||||
|
|
||||||
|
setting :root, default: "/tmp"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
let(:container) do
|
||||||
|
klass.new
|
||||||
|
end
|
||||||
|
|
||||||
|
it "exposes custom config" do
|
||||||
|
expect(container.config.root).to eql("/tmp")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -13,6 +13,10 @@ Dir[Pathname(__FILE__).dirname.join("support/**/*.rb").to_s].sort.each do |file|
|
||||||
require file
|
require file
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if ENV["DRY_CONFIGURABLE"] == "true"
|
||||||
|
require "dry/configurable"
|
||||||
|
end
|
||||||
|
|
||||||
require "dry/container"
|
require "dry/container"
|
||||||
require "dry/container/stub"
|
require "dry/container/stub"
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue