Class: Goru::Channel

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

Instance Attribute Summary collapse

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 Attribute Details

#observer=(value) ⇒ Object (writeonly)

public


16
17
18
# File 'lib/goru/channel.rb', line 16

def observer=(value)
  @observer = value
end

Instance Method Details

#<<(message) ⇒ Object

public


20
21
22
23
24
# File 'lib/goru/channel.rb', line 20

def <<(message)
  raise "closed" if @closed
  @messages << message
  @observers.each(&:channel_received)
end

#add_observer(observer) ⇒ Object

public


86
87
88
# File 'lib/goru/channel.rb', line 86

def add_observer(observer)
  @observers << observer
end

#any?Boolean

public

Returns:

  • (Boolean)


36
37
38
# File 'lib/goru/channel.rb', line 36

def any?
  @messages.any?
end

#clearObject

public


74
75
76
# File 'lib/goru/channel.rb', line 74

def clear
  @messages.clear
end

#closeObject

public


60
61
62
63
# File 'lib/goru/channel.rb', line 60

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

#closed?Boolean

public

Returns:

  • (Boolean)


54
55
56
# File 'lib/goru/channel.rb', line 54

def closed?
  @closed == true
end

#empty?Boolean

public

Returns:

  • (Boolean)


42
43
44
# File 'lib/goru/channel.rb', line 42

def empty?
  @messages.empty?
end

#full?Boolean

public

Returns:

  • (Boolean)


48
49
50
# File 'lib/goru/channel.rb', line 48

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

#lengthObject

public


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

def length
  @messages.length
end

#readObject

public


28
29
30
31
32
# File 'lib/goru/channel.rb', line 28

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

#remove_observer(observer) ⇒ Object

public


92
93
94
# File 'lib/goru/channel.rb', line 92

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

#reopenObject

public


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

def reopen
  @closed = false
  @observers.each(&:channel_reopened)
end