Class: Unobtainium::Runtime
- Inherits:
-
Object
- Object
- Unobtainium::Runtime
- Includes:
- Singleton
- Defined in:
- lib/unobtainium/runtime.rb
Overview
The Runtime class is a singleton scoped to destroy itself when script execution stops. It’s also an object map, which will destroy all object it contains when it destroys itself.
Therefore, it can be used as a way to register object instances for destruction at script end.
Instance Method Summary collapse
-
#[](name) ⇒ Object
Similar to :fetch, but always returns nil for an object that could not be found.
-
#fetch(name, default = nil) ⇒ Object
Returns the object with the given name, or the default value if no such object exists.
-
#has?(name) ⇒ Boolean
Does an object with the given name exist?.
-
#initialize ⇒ Runtime
constructor
Initializer.
-
#length ⇒ Object
Number of objects stored in the object map.
-
#set(name, object, destructor = nil) ⇒ Object
Store the given object under the given name.
-
#set_with(name, destructor = nil, &block) ⇒ Object
Store the object returned by the block, if any.
-
#unset(name) ⇒ Object
Unsets (and destroys) any object found under the given name.
Constructor Details
#initialize ⇒ Runtime
Initializer
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/unobtainium/runtime.rb', line 25 def initialize @objects = {} # Create our own finalizer ObjectSpace.define_finalizer(self) do @objects.keys.each do |key| unset(key) end end end |
Instance Method Details
#[](name) ⇒ Object
Similar to :fetch, but always returns nil for an object that could not be found.
104 105 106 |
# File 'lib/unobtainium/runtime.rb', line 104 def [](name) return @objects[name] end |
#fetch(name, default = nil) ⇒ Object
Returns the object with the given name, or the default value if no such object exists.
97 98 99 |
# File 'lib/unobtainium/runtime.rb', line 97 def fetch(name, default = nil) return @objects.fetch(name, default) end |
#has?(name) ⇒ Boolean
Does an object with the given name exist?
44 45 46 |
# File 'lib/unobtainium/runtime.rb', line 44 def has?(name) return @objects.key?(name) end |
#length ⇒ Object
Number of objects stored in the object map
38 39 40 |
# File 'lib/unobtainium/runtime.rb', line 38 def length return @objects.length end |
#set(name, object, destructor = nil) ⇒ Object
Store the given object under the given name. This overwrites any objects already stored under that name, which are destroyed before the new object is stored.
If a destructor is passed, it is used to destroy the new object only. If no destructor is passed and the object responds to a :destroy method, that method is called.
56 57 58 59 60 61 62 |
# File 'lib/unobtainium/runtime.rb', line 56 def set(name, object, destructor = nil) unset(name) @objects[name] = [object, destructor] return object end |
#set_with(name, destructor = nil, &block) ⇒ Object
Store the object returned by the block, if any. If no object is returned or no block is given, this function does nothing.
Otherwise it works much like :set above.
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/unobtainium/runtime.rb', line 69 def set_with(name, destructor = nil, &block) object = nil if not block.nil? object = yield end if object.nil? return end return set(name, object, destructor) end |
#unset(name) ⇒ Object
Unsets (and destroys) any object found under the given name.
84 85 86 87 88 89 90 91 92 |
# File 'lib/unobtainium/runtime.rb', line 84 def unset(name) if not @objects.key?(name) return end obj, dtor = @objects[name] @objects.delete(name) destroy(obj, dtor) end |