Class: Tango::Resource::Buffer
- Inherits:
-
Object
- Object
- Tango::Resource::Buffer
- Defined in:
- lib/tango/resource/buffer.rb
Overview
Resource buffer
Instance Method Summary collapse
-
#fill(type, resource) ⇒ Object
Fill buffer with a resource.
-
#initialize(size = 500) ⇒ Tango::Resource::Buffer
constructor
Constructor of the Buffer.
-
#register(type, &release_callback) ⇒ Object
Register new type of resource to be buffered.
-
#release_all ⇒ Object
Release all registered buffers.
Constructor Details
#initialize(size = 500) ⇒ Tango::Resource::Buffer
Constructor of the Buffer
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/tango/resource/buffer.rb', line 13 def initialize( size = 500 ) # Set max size of the buffer @size = size # Buffer storage for resources @resources = {} # Container of callbacks for each registered resource type @callbacks = {} end |
Instance Method Details
#fill(type, resource) ⇒ Object
Fill buffer with a resource
42 43 44 45 46 47 48 49 50 |
# File 'lib/tango/resource/buffer.rb', line 42 def fill( type, resource ) raise ArgumentError, "Trying to fill object with unregistered type" unless @resources.keys.include?( type ) # Append resource to the buffer @resources[type] << resource # Release the buffer if buffer size exceeded release( type ) if @resources[type].count >= @size end |
#register(type, &release_callback) ⇒ Object
Register new type of resource to be buffered
29 30 31 32 33 34 35 36 |
# File 'lib/tango/resource/buffer.rb', line 29 def register( type, &release_callback ) raise ArgumentError, "No release callback given" unless block_given? @resources[type] = [] @callbacks[type] = release_callback end |
#release_all ⇒ Object
Release all registered buffers
53 54 55 |
# File 'lib/tango/resource/buffer.rb', line 53 def release_all @resources.keys.each { |type| release( type ) } end |