Class: Goru::Channel
- Inherits:
-
Object
- Object
- Goru::Channel
- Defined in:
- lib/goru/channel.rb
Instance Method Summary collapse
-
#<<(message) ⇒ Object
[public].
-
#add_observer(observer) ⇒ Object
[public].
-
#any? ⇒ Boolean
[public].
-
#clear ⇒ Object
[public].
-
#close ⇒ Object
[public].
-
#closed? ⇒ Boolean
[public].
-
#empty? ⇒ Boolean
[public].
-
#full? ⇒ Boolean
[public].
-
#initialize(size: nil) ⇒ Channel
constructor
A new instance of Channel.
-
#length ⇒ Object
[public].
-
#read ⇒ Object
[public].
-
#remove_observer(observer) ⇒ Object
[public].
Constructor Details
#initialize(size: nil) ⇒ Channel
Returns a new instance of Channel.
7 8 9 10 11 12 |
# File 'lib/goru/channel.rb', line 7 def initialize(size: nil) @size = size @messages = [] @closed = false @observers = Set.new end |
Instance Method Details
#<<(message) ⇒ Object
- public
16 17 18 19 20 |
# File 'lib/goru/channel.rb', line 16 def <<() raise "closed" if @closed @messages << @observers.each(&:channel_received) end |
#add_observer(observer) ⇒ Object
- public
75 76 77 |
# File 'lib/goru/channel.rb', line 75 def add_observer(observer) @observers << observer end |
#any? ⇒ Boolean
- public
32 33 34 |
# File 'lib/goru/channel.rb', line 32 def any? @messages.any? end |
#clear ⇒ Object
- public
63 64 65 |
# File 'lib/goru/channel.rb', line 63 def clear @messages.clear end |
#close ⇒ Object
- public
56 57 58 59 |
# File 'lib/goru/channel.rb', line 56 def close @closed = true @observers.each(&:channel_closed) end |
#closed? ⇒ Boolean
- public
50 51 52 |
# File 'lib/goru/channel.rb', line 50 def closed? @closed == true end |
#empty? ⇒ Boolean
- public
38 39 40 |
# File 'lib/goru/channel.rb', line 38 def empty? @messages.empty? end |
#full? ⇒ Boolean
- public
44 45 46 |
# File 'lib/goru/channel.rb', line 44 def full? !!@size && @messages.size == @size end |
#length ⇒ Object
- public
69 70 71 |
# File 'lib/goru/channel.rb', line 69 def length @messages.length end |
#read ⇒ Object
- public
24 25 26 27 28 |
# File 'lib/goru/channel.rb', line 24 def read = @messages.shift @observers.each(&:channel_read) end |
#remove_observer(observer) ⇒ Object
- public
81 82 83 |
# File 'lib/goru/channel.rb', line 81 def remove_observer(observer) @observers.delete(observer) end |