Class: Goru::Channel

Inherits:
Object
  • Object
show all
Defined in:
lib/goru/channel.rb

Instance Method Summary collapse

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 <<(message)
  raise "closed" if @closed
  @messages << message
  @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

Returns:

  • (Boolean)


32
33
34
# File 'lib/goru/channel.rb', line 32

def any?
  @messages.any?
end

#clearObject

public


63
64
65
# File 'lib/goru/channel.rb', line 63

def clear
  @messages.clear
end

#closeObject

public


56
57
58
59
# File 'lib/goru/channel.rb', line 56

def close
  @closed = true
  @observers.each(&:channel_closed)
end

#closed?Boolean

public

Returns:

  • (Boolean)


50
51
52
# File 'lib/goru/channel.rb', line 50

def closed?
  @closed == true
end

#empty?Boolean

public

Returns:

  • (Boolean)


38
39
40
# File 'lib/goru/channel.rb', line 38

def empty?
  @messages.empty?
end

#full?Boolean

public

Returns:

  • (Boolean)


44
45
46
# File 'lib/goru/channel.rb', line 44

def full?
  !!@size && @messages.size == @size
end

#lengthObject

public


69
70
71
# File 'lib/goru/channel.rb', line 69

def length
  @messages.length
end

#readObject

public


24
25
26
27
28
# File 'lib/goru/channel.rb', line 24

def read
  message = @messages.shift
  @observers.each(&:channel_read)
  message
end

#remove_observer(observer) ⇒ Object

public


81
82
83
# File 'lib/goru/channel.rb', line 81

def remove_observer(observer)
  @observers.delete(observer)
end