Class: LapisLazuli::Runtime
- Inherits:
-
Object
- Object
- LapisLazuli::Runtime
- Includes:
- Singleton
- Defined in:
- lib/lapis_lazuli/runtime.rb
Overview
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.
Instance Method Summary collapse
- #get(name) ⇒ Object
- #has?(name) ⇒ Boolean
-
#initialize ⇒ Runtime
constructor
A new instance of Runtime.
-
#length ⇒ Object
Number of objects stored in the Runtime.
- #set(world, name, object, destructor = nil) ⇒ Object
- #set_if(world, name, destructor = nil, &block) ⇒ Object
-
#unset(name) ⇒ Object
Remove an object from the Runtime.
Constructor Details
#initialize ⇒ Runtime
Returns a new instance of Runtime.
18 19 20 |
# File 'lib/lapis_lazuli/runtime.rb', line 18 def initialize @objects = {} end |
Instance Method Details
#get(name) ⇒ Object
64 65 66 |
# File 'lib/lapis_lazuli/runtime.rb', line 64 def get(name) return @objects[name] end |
#has?(name) ⇒ Boolean
28 29 30 |
# File 'lib/lapis_lazuli/runtime.rb', line 28 def has?(name) return @objects.has_key? name end |
#length ⇒ Object
Number of objects stored in the Runtime
24 25 26 |
# File 'lib/lapis_lazuli/runtime.rb', line 24 def length return @objects.keys.length end |
#set(world, name, object, destructor = nil) ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# 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) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# 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) ⇒ Object
Remove an object from the Runtime
60 61 62 |
# File 'lib/lapis_lazuli/runtime.rb', line 60 def unset(name) @objects.delete(name) end |