-
-
Save AMHOL/1bd1114fe93750153ca883c9e867e414 to your computer and use it in GitHub Desktop.
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
gemfile(true) do | |
gem 'dry-configurable' | |
gem 'dry-container' | |
gem 'dry-auto_inject' | |
end | |
# Gem | |
module MyGem | |
extend Dry::Configurable | |
setting :container | |
def self.inject | |
@_inject ||= Dry::AutoInject(config.container) | |
end | |
def self.component | |
Component.new | |
end | |
def self.finalize! | |
# require gem components | |
const_set( | |
:Component, | |
Class.new { include MyGem.inject[:some_dependency] } | |
) | |
end | |
end | |
# App | |
class MyApp | |
extend Dry::Container::Mixin | |
register(:some_dependency) { SomeDependency.new } | |
end | |
class SomeDependency; end | |
MyGem.configure do |config| | |
config.container = MyApp | |
end | |
MyGem.finalize! | |
MyGem.component | |
# => => #<MyGem::Component:0x0055719df15b30 @some_dependency=#<SomeDependency:0x0055719df15cc0>> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment