Class: Tango::Resource::Buffer

Inherits:
Object
  • Object
show all
Defined in:
lib/tango/resource/buffer.rb

Overview

Resource buffer

Author:

  • Mckomo

Instance Method Summary collapse

Constructor Details

#initialize(size = 500) ⇒ Tango::Resource::Buffer

Constructor of the Buffer

Parameters:

  • size (Integer) (defaults to: 500)


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

Parameters:

  • type (Symbol)
  • resource (Object)

Raises:

  • (ArgumentError)


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

Parameters:

  • type (Symbol)
  • release_callback (Proc)

Raises:

  • (ArgumentError)


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_allObject

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