class LapisLazuli::Runtime
Simple singleton class (that therefore lives for the duration of the test's run time for managing objects whose lifetime should also be this long.
Public Class Methods
new()
click to toggle source
# File lib/lapis_lazuli/runtime.rb, line 18 def initialize @objects = {} end
Private Class Methods
destroy(world, name, destructor)
click to toggle source
# File lib/lapis_lazuli/runtime.rb, line 70 def self.destroy(world, name, destructor) Proc.new do # Try to run destroy on the object itself obj = Runtime.instance.get(name) #world.log.debug("Destroying #{name}") # Do not destroy the logger until everything else has been stopped if name == :logger and Runtime.instance.length > 1 #world.log.debug("This is the logger, wait until everything is closed") break end Runtime.instance.unset(name) if obj.respond_to?(:destroy) obj.send(:destroy, world) break end # If a destructor is given, call that. if not destructor.nil? return destructor.call(world) end # Next, try a has_foo?/foo.destroy combination if world.respond_to? "has_#{name}?" and world.respond_to? name if world.send("has_#{name}?") return world.send(name).destroy(world) end return false end # If it only responds to a destroy function, then we can just # call that. if world.respond_to? name return world.send(name).destroy(world) end # If the object has stream/socket functions close ends the connection if obj.respond_to? :close return obj.send(:close) end # If all else fails, we have to log an error. We can't rely # on log existing in world, though... message = "No destructor available for #{name}." if world.respond_to? :log world.log.info(message) else puts message end end end
Public Instance Methods
get(name)
click to toggle source
# File lib/lapis_lazuli/runtime.rb, line 64 def get(name) return @objects[name] end
has?(name)
click to toggle source
# File lib/lapis_lazuli/runtime.rb, line 28 def has?(name) return @objects.has_key? name end
length()
click to toggle source
Number of objects stored in the Runtime
# File lib/lapis_lazuli/runtime.rb, line 24 def length return @objects.keys.length end
set(world, name, object, destructor = nil)
click to toggle source
# File lib/lapis_lazuli/runtime.rb, line 32 def set(world, name, object, destructor = nil) if @objects.has_key? name Runtime.destroy(world, name, destructor) end # Register a finalizer, so we can clean up the proxy again ObjectSpace.define_finalizer(self, Runtime.destroy(world, name, destructor)) @objects[name] = object end
set_if(world, name, destructor = nil, &block)
click to toggle source
# File lib/lapis_lazuli/runtime.rb, line 43 def set_if(world, name, destructor = nil, &block) if @objects.has_key? name return @objects[name] end obj = nil if !block.nil? obj = block.call end set(world, name, obj, destructor) return obj end
unset(name)
click to toggle source
Remove an object from the Runtime
# File lib/lapis_lazuli/runtime.rb, line 60 def unset(name) @objects.delete(name) end