Instantly share code, notes, and snippets.

Embed
What would you like to do?
Dumb DI container
class DIContainer
def configure
yield self
end
def method_missing(key, val)
if key =~ /=$/
key = key.to_s.chop
(class << self; self; end).class_eval do
attr_accessor key
end
__send__(:"#{key}=", val)
else
super
end
end
end
def configure
Container.new.tap do |c|
yield c
c.freeze if freeze
end
end
module A
extend self
def shout(foo: DI.foo)
foo.shout
end
end
module Foo
extend self
def shout;"the real foo";end
end
module FakeFoo
extend self
def shout;"fake foo!";end
end
DI = configure do |c|
c.foo = Foo
end
A.shout
DI.configure do |c|
c.foo = FakeFoo
end
A.shout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment