From: Christian Neukirchen Date: 2006-04-30T18:18:06+09:00 Subject: Re: [ANN] MinDI 0.3 Joel VanderWerf writes: > Using the InjectableContainer module for "dynamic" or "fallback" > injection, using method_missing: > > require 'mindi' > > class Transformer > def transform string > string.gsub(pattern, &replacement) > end > end > > class TransformerContainer > include MinDI::InjectableContainer > > pattern { /foo/ } > replacement { proc {|match| match.upcase } } > transformer { Transformer.new } > transform { |str| transformer.transform(str) } > end > > cont = TransformerContainer.new > s1 = cont.transform("fo foo fee") > s2 = cont.transform("fo foo fee") > p s1 # ==> "fo FOO fee" > p s1.equal?(s2) # ==> true > > > Note that the Transformer class is written without explicitly linking up > to services (either in initialize or in setters). It just assumes that > the services will be defined in the container. So, if I get that right... the Transformer instance gets extended with the TransformerContainer automatically? Or how does it know about "pattern" and "replacement"? Isn't that a clear and obvious violation of encapsulation? And won't it hide lots of bugs due to typos or doubly defined methods? > Note also that the #transform service is a multiton service, and (like > singleton services) it caches its value for each argument. > > -- > vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407 > -- Christian Neukirchen http://chneukirchen.org