Class: Unobtainium::Runtime

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeRuntime

Initializer



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/unobtainium/runtime.rb', line 25

def initialize
  @objects = {}

  # Create our own finalizer
  ObjectSpace.define_finalizer(self) do
    # :nocov:
    @objects.keys.each do |key|
      delete(key)
    end
    # :nocov:
  end
end

Instance Method Details

#[](name) ⇒ Object

Similar to ‘#fetch`, but always returns nil for an object that could not be found.

Parameters:

  • name (String, Symbol)

    name or label for the object to retrieve

Returns:

  • (Object)

    the object matching the name/label, or the default value.



154
155
156
157
158
159
160
# File 'lib/unobtainium/runtime.rb', line 154

def [](name)
  val = @objects[name]
  if val.nil?
    return nil
  end
  return val[0]
end

#delete(name) ⇒ Object

Deletes (and destroys) any object found under the given name.

Parameters:

  • name (String, Symbol)

    name or label for the object to store



121
122
123
124
125
126
127
128
129
# File 'lib/unobtainium/runtime.rb', line 121

def delete(name)
  if not @objects.key?(name)
    return
  end

  obj, dtor = @objects[name]
  @objects.delete(name)
  destroy(obj, dtor)
end

#fetch(name, default = nil) ⇒ Object

Returns the object with the given name, or the default value if no such object exists.

Parameters:

  • name (String, Symbol)

    name or label for the object to retrieve

  • default (Object) (defaults to: nil)

    default value to return if no object is found for name or label.

Returns:

  • (Object)

    the object matching the name/label, or the default value.



139
140
141
142
143
144
145
146
# File 'lib/unobtainium/runtime.rb', line 139

def fetch(name, default = nil)
  return @objects.fetch(name)[0]
rescue KeyError
  if default.nil?
    raise
  end
  return default
end

#has?(name) ⇒ Boolean

Returns does an object with the given name exist?.

Parameters:

  • name (String, Symbol)

    name or label for an object

Returns:

  • (Boolean)

    does an object with the given name exist?



47
48
49
# File 'lib/unobtainium/runtime.rb', line 47

def has?(name)
  return @objects.key?(name)
end

#lengthInteger

Returns number of objects stored in the object map.

Returns:

  • (Integer)

    number of objects stored in the object map



40
41
42
# File 'lib/unobtainium/runtime.rb', line 40

def length
  return @objects.length
end

#store(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.

Parameters:

  • name (String, Symbol)

    name or label for the object to store

  • object (Object)

    the object to store

  • destructor (Func) (defaults to: nil)

    a custom destructor accepting the object as its parameter.

Returns:

  • (Object)

    the stored object



65
66
67
68
69
70
71
# File 'lib/unobtainium/runtime.rb', line 65

def store(name, object, destructor = nil)
  delete(name)

  @objects[name] = [object, destructor]

  return object
end

#store_if(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.

Like ‘#store`, but only stores the object if none exists for that key yet.

Parameters:

  • name (String, Symbol)

    name or label for the object to store

  • object (Object)

    the object to store

  • destructor (Func) (defaults to: nil)

    a custom destructor accepting the object as its parameter.

Returns:

  • (Object)

    the stored object



100
101
102
103
104
105
# File 'lib/unobtainium/runtime.rb', line 100

def store_if(name, object, destructor = nil)
  if has?(name)
    return self[name]
  end
  return store(name, object, destructor)
end

#store_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 ‘#store`.

Parameters:

  • name (String, Symbol)

    name or label for the object to store

  • destructor (Func) (defaults to: nil)

    a custom destructor accepting the object as its parameter.

  • block (Func)

    a block returning the created object.

Returns:

  • (Object)

    the stored object



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/unobtainium/runtime.rb', line 84

def store_with(name, destructor = nil, &block)
  object = nil
  if not block.nil?
    object = yield
  end

  if object.nil?
    return
  end

  return store(name, object, destructor)
end

#store_with_if(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 ‘#store`.

Like ‘#store_if`, but as a block version similar to `#store_with`.

Parameters:

  • name (String, Symbol)

    name or label for the object to store

  • destructor (Func) (defaults to: nil)

    a custom destructor accepting the object as its parameter.

  • block (Func)

    a block returning the created object.

Returns:

  • (Object)

    the stored object



110
111
112
113
114
115
# File 'lib/unobtainium/runtime.rb', line 110

def store_with_if(name, destructor = nil, &block)
  if has?(name)
    return self[name]
  end
  return store_with(name, destructor, &block)
end