From: Joel VanderWerf Date: 2004-11-14T11:47:16+09:00 Subject: Re: [ANN] Copland to Needle article on RubyGarden (LONG) Jamis Buck wrote: > Joel VanderWerf wrote: ... >> There are other service models that are very easy to construct with >> methods, but I'm not aware of an equivalent construct in Needle: >> >> def printer(kind) >> @printers ||= {} >> @printers[kind] = Printer.new(kind) >> end >> >> def printer_for_code_files >> @printer_for_code_files ||= printer(:monochrome) >> end >> >> def printer_for_images >> @printer_for_images ||= printer(:color) >> end >> >> It might be possible to do the above in Needle using Pipelines.[2] > > > Hmm. I wouldn't call this a service model so much as a factory service. > In fact, I have a new favorite way of doing this kind of thing, as of > this morning. To complicate things a bit, suppose that the Printer class > needs to be dependency injected (with a logger instance, for example), > and the following approach really shines: > > reg.define do |b| > b.printer do |ctr,info| > logger = ctr[:logs].get( info ) > lambda { |kind| Printer.new( logger, kind ) } > end > > b.code_printer { |ctr,| ctr.printer.call( :monochrome ) } > b.image_printer { |ctr,| ctr.printer.call( :color ) } > end > > I've actually incorporated this approach into various places in the > upcoming new release of Net::SSH, and it works beautifully. That captures the parameterization, but it doesn't capture the multiton[1] service model of my code. A unique instance is generated for each distinct argument list (distinct in the sense of hash keys, which is to say #eql?). What's the most natural way to do that with Needle? -- [1] http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/67899 (ruby-talk.org is down right now).