Last active
February 21, 2018 20:08
-
-
Save streppa/487bb601760a50709ccef8aaa1d556fa to your computer and use it in GitHub Desktop.
An contrived example [dry-container, dry-configurable, dry-auto_inject]
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
#!/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