Skip to content

Instantly share code, notes, and snippets.

@streppa
Last active February 21, 2018 20:08
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 streppa/487bb601760a50709ccef8aaa1d556fa to your computer and use it in GitHub Desktop.
Save streppa/487bb601760a50709ccef8aaa1d556fa to your computer and use it in GitHub Desktop.
An contrived example [dry-container, dry-configurable, dry-auto_inject]
#!/usr/bin/env ruby
require 'bundler/inline'
gemfile(true) do
source "https://rubygems.org"
gem 'dry-system', '0.8.0'
gem 'dry-configurable'
gem 'dry-container'
gem 'dry-auto_inject', '0.4.4'
#gem 'dry-auto_inject'
end
puts "\n"
require 'dry-auto_inject'
require 'dry-configurable'
require 'dry-container'
class SystemContainer
extend Dry::Container::Mixin
register(:namespaces, -> { Dry::Container.new }, memoize: true, call: false)
end
# Autoloaded from something like system/boot maybe?
SystemContainer[:namespaces].register :persistence do
Dry::Container::Namespace.new :persistence do
new_container = Dry::Container.new
new_container.register :verb_past_tense, -> { self.verb_past_tense }
new_container.register :singular_noun, -> { self.singular_noun }
new_container.register :place, -> { self.place }
register :some_config, new_container
register :some_instantiation do # Kinda like creating a ROM.container?
config = self['persistence.some_config']
'I created something that %s from %s onto a %s' % [
config[:verb_past_tense], config[:place], config[:singular_noun]
]
end
end
end
ImportSystemContainer = Dry::AutoInject(SystemContainer)
class MadLib
extend Dry::Configurable
include Dry::Container::Mixin
include ImportSystemContainer[:namespaces]
def self.attrs
%i| verb_past_tense singular_noun place |
end
attrs.each do |attr|
setting attr
attr_accessor attr
end
def initialize(args = {})
super
self.class.attrs.each do |attr|
send("#{attr}=".to_sym, args[attr] || self.class.config[attr] || raise(ArgumentError, "'#{attr}' hasn't been configured"))
#puts "#{attr}=" + send(attr).to_s
end
# In this example there's only one namespace so far.
import namespaces[:persistence]
register('persistence.madlib', -> { self['persistence.some_instantiation'] })
end
def madlib
self['persistence.madlib']
end
end
### No more infrastructure...
begin
puts "'place' is deliberately unconfigured."
client = MadLib.new verb_past_tense: 'throttled', singular_noun: 'firetruck'
puts client.madlib
puts ''
rescue ArgumentError => e
puts e.message
puts '*** ArgumentError caught. Oh well, moving on...'
puts ''
end
MadLib.configure do |config|
config.place = 'the local diner'
end
puts "'place' is configured as a default. "
client = MadLib.new verb_past_tense: 'throttled', singular_noun: 'firetruck'
puts client.madlib
puts ''
puts "'place' stays the same while the rest change"
new_client = MadLib.new verb_past_tense: 'dropped', singular_noun: 'volcano'
puts new_client.madlib
puts ''
puts "Here we explicitly set 'place'"
newer_client = MadLib.new place: 'the mayors boudoir', verb_past_tense: 'flooded', singular_noun: 'penthouse'
puts newer_client.madlib
puts ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment