Class: Tamashii::Manager::ChannelPool
- Inherits:
-
Hash
- Object
- Hash
- Tamashii::Manager::ChannelPool
- Defined in:
- lib/tamashii/manager/channel_pool.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#idles ⇒ Object
readonly
Returns the value of attribute idles.
Instance Method Summary collapse
- #available? ⇒ Boolean
- #create! ⇒ Object
- #idle(channel_id = nil) ⇒ Object
-
#initialize(size = 10) ⇒ ChannelPool
constructor
A new instance of ChannelPool.
- #ready(channel) ⇒ Object
Constructor Details
#initialize(size = 10) ⇒ ChannelPool
Returns a new instance of ChannelPool.
9 10 11 12 13 14 |
# File 'lib/tamashii/manager/channel_pool.rb', line 9 def initialize(size = 10) @idles = [] @ptr = 1 size.times { create! } end |
Instance Attribute Details
#idles ⇒ Object (readonly)
Returns the value of attribute idles.
7 8 9 |
# File 'lib/tamashii/manager/channel_pool.rb', line 7 def idles @idles end |
Instance Method Details
#available? ⇒ Boolean
37 38 39 |
# File 'lib/tamashii/manager/channel_pool.rb', line 37 def available? !@idles.empty? end |
#create! ⇒ Object
16 17 18 19 20 21 |
# File 'lib/tamashii/manager/channel_pool.rb', line 16 def create! channel = Channel.new(@ptr) @idles << channel @ptr += 1 channel end |
#idle(channel_id = nil) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/tamashii/manager/channel_pool.rb', line 23 def idle(channel_id = nil) return @idles.first if channel_id.nil? return unless self[channel_id]&.empty? @idles << self[channel_id] self[channel_id] = nil end |
#ready(channel) ⇒ Object
30 31 32 33 34 35 |
# File 'lib/tamashii/manager/channel_pool.rb', line 30 def ready(channel) return if channel.empty? self[channel.id] = channel @idles.delete(channel) if @idles.include?(channel) channel end |