From 6d2eac2c3dd745a84d963ee90fc29ab775b9918c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Busqu=C3=A9?= Date: Wed, 3 Jul 2019 07:50:55 +0200 Subject: [PATCH] Add container extension Adds a `container` setting to `WebPipe::Conn` configuration. Besides the setting, a `.container` reader attribute is added to it. This extension is meant to be a building block for other extensions. ```ruby WebPipe.load_extensions(:container) Container = {'foo' => 'bar'}.freeze WebPipe.config.container = Container WebPipe.container['foo'] # => 'bar' ``` --- Gemfile.lock | 1 + README.md | 3 +++ lib/web_pipe.rb | 4 +++ lib/web_pipe/conn.rb | 2 ++ .../extensions/container/container.rb | 25 +++++++++++++++++++ spec/extensions/container/container_spec.rb | 23 +++++++++++++++++ web_pipe.gemspec | 1 + 7 files changed, 59 insertions(+) create mode 100644 lib/web_pipe/extensions/container/container.rb create mode 100644 spec/extensions/container/container_spec.rb diff --git a/Gemfile.lock b/Gemfile.lock index ffbc518..761e092 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,6 +2,7 @@ PATH remote: . specs: web_pipe (0.1.0) + dry-configurable (~> 0.8) dry-initializer (~> 3.0) dry-monads (~> 1.2) dry-struct (~> 1.0) diff --git a/README.md b/README.md index cf9d1a5..37479ee 100644 --- a/README.md +++ b/README.md @@ -271,6 +271,9 @@ extensions (click on each name for details on the usage): - [dry-view](lib/web_pipe/extensions/dry_view/dry_view.rb): Integration with [`dry-view`](https://dry-rb.org/gems/dry-view/) rendering system. +- [container](lib/web_pipe/extensions/container/container.rb): Allows + configuring a container to resolve dependencies from + `WebPipe::Conn`. ## Current status diff --git a/lib/web_pipe.rb b/lib/web_pipe.rb index 8654b6e..f109708 100644 --- a/lib/web_pipe.rb +++ b/lib/web_pipe.rb @@ -19,4 +19,8 @@ def self.call(*args) register_extension :dry_view do require 'web_pipe/extensions/dry_view/dry_view' end + + register_extension :container do + require 'web_pipe/extensions/container/container' + end end \ No newline at end of file diff --git a/lib/web_pipe/conn.rb b/lib/web_pipe/conn.rb index b95e97f..01a03ee 100644 --- a/lib/web_pipe/conn.rb +++ b/lib/web_pipe/conn.rb @@ -1,4 +1,5 @@ require 'dry/struct' +require 'dry/configurable' require 'web_pipe/conn_support/types' require 'web_pipe/conn_support/errors' require 'web_pipe/conn_support/headers' @@ -30,6 +31,7 @@ module WebPipe # taint class Conn < Dry::Struct include ConnSupport::Types + extend Dry::Configurable # @!attribute [r] env # diff --git a/lib/web_pipe/extensions/container/container.rb b/lib/web_pipe/extensions/container/container.rb new file mode 100644 index 0000000..3e79b59 --- /dev/null +++ b/lib/web_pipe/extensions/container/container.rb @@ -0,0 +1,25 @@ +module WebPipe + # Adds a `container` setting to {WebPipe::Conn} configuration. + # + # Besides the setting, a `.container` reader attribute is added to + # {WebPipe::Conn}. + # + # This extension is meant to be a building block for other + # extensions. + # + # @example + # WebPipe.load_extensions(:container) + # + # Container = {'foo' => 'bar'}.freeze + # + # WebPipe.config.container = Container + # WebPipe.container['foo'] # => 'bar' + class Conn < Dry::Struct + # Container with nothing registered. + EMPTY_CONTAINER = {}.freeze + + setting(:container, EMPTY_CONTAINER, reader: true) do |c| + Types::Container[c] + end + end +end diff --git a/spec/extensions/container/container_spec.rb b/spec/extensions/container/container_spec.rb new file mode 100644 index 0000000..cf792da --- /dev/null +++ b/spec/extensions/container/container_spec.rb @@ -0,0 +1,23 @@ +require 'spec_helper' +require 'support/env' +require 'web_pipe/conn' + +RSpec.describe WebPipe::Conn do + before do + WebPipe.load_extensions(:container) + end + + let(:container) { {'foo' => 'bar'}.freeze } + + before { WebPipe::Conn.config.container = container } + + describe '#config' do + it "has a 'container' setting" do + expect(WebPipe::Conn.config.container).to be(container) + end + end + + it "has a 'container' reader" do + expect(WebPipe::Conn.container).to be(container) + end +end \ No newline at end of file diff --git a/web_pipe.gemspec b/web_pipe.gemspec index 66d2dac..ac70126 100644 --- a/web_pipe.gemspec +++ b/web_pipe.gemspec @@ -40,6 +40,7 @@ Gem::Specification.new do |spec| spec.add_runtime_dependency "dry-types", "~> 1.1" spec.add_runtime_dependency "dry-struct", "~> 1.0" spec.add_runtime_dependency "dry-initializer", "~> 3.0" + spec.add_runtime_dependency "dry-configurable", "~> 0.8" spec.add_development_dependency "bundler", "~> 1.17" spec.add_development_dependency "rake", "~> 10.0"