Skip to content

Instantly share code, notes, and snippets.

@gottfrois
Created January 4, 2018 15:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gottfrois/5e8edd540910c3ee8276eb2c76d8ff1a to your computer and use it in GitHub Desktop.
Save gottfrois/5e8edd540910c3ee8276eb2c76d8ff1a to your computer and use it in GitHub Desktop.
Dry-system component example using gems
# 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