Skip to content

Instantly share code, notes, and snippets.

@AMHOL

AMHOL/ex.rb Secret

Created August 18, 2016 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AMHOL/1bd1114fe93750153ca883c9e867e414 to your computer and use it in GitHub Desktop.
Save AMHOL/1bd1114fe93750153ca883c9e867e414 to your computer and use it in GitHub Desktop.
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