It would be nice to be able to pass a block to a service point that has been registered. Say for example you have some
class that needs to write to a file (yes, this is a silly example but hopefully simple enough to be clear):
class MyClass
def do_something_to_a_file
REGISTRY.file("my_file") do |file|
...
end
end
end
And in your services.rb file:
REGISTRY = Needle::Registry.new
REGISTRY.register(:file, :model => :prototype) do |container, point, file_name, block|
if block.nil?
File.open(file_name)
else
File.open(file_name, &block)
end
end
Not sure what the best way to do this is...but one approach is as follows:
1. Update container::method_missing( sym, * args, &block )
2. Update container::get( name, *args, &block )
3. Update service-point::instance( *args, &block )
4. And then the call method in multiton (andother similar classes):
def call( container, point, *args, &block )
unless @is_cached[ args ]
@mutex.synchronize do
unless @is_cached[ args ]
a_proc = Proc.new(&block)
args.push(a_proc)
@cached[ args ] = succ.call( container, point, *args)
@is_cached[ args ] = true
end
end
end
|