Created
January 4, 2018 15:41
-
-
Save gottfrois/5e8edd540910c3ee8276eb2c76d8ff1a to your computer and use it in GitHub Desktop.
Dry-system component example using gems
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
# foo/lib/foo.rb | |
require 'dry/system' | |
require 'foo/version' | |
module Foo | |
Dry::System.register_provider( | |
:foo, | |
boot_path: Pathname(__dir__).join('foo/components').realpath | |
) | |
end | |
# foo/lib/foo/components/logger.rb | |
Dry::System.register_component(:logger, provider: :foo) do | |
init do | |
require 'logger' | |
end | |
start do | |
register(:logger, Logger.new($stdout)) | |
end | |
end | |
# Main application Gemfile | |
gem 'foo', path: './lib/foo' | |
# Main application system container myapp/system/myapp/container.rb | |
require 'dry/system/container' | |
module Myapp | |
class Container < Dry::System::Container | |
boot :logger, from: :foo | |
configure do |config| | |
config.name :myapp | |
... | |
end | |
load_paths!('lib') | |
end | |
Container.finalize! | |
end | |
# Which allows in console to do: | |
> require 'myapp' | |
true | |
> Myapp::Container[:logger].info "hello world!" | |
I, [2018-01-04T16:35:36.531424 #7230] INFO -- : hello world! | |
=> true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment