Class: Webmidi::Port::Map

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/webmidi/port/map.rb

Instance Method Summary collapse

Constructor Details

#initialize(ports = [], mutable: true) ⇒ Map

Returns a new instance of Map.



8
9
10
11
12
13
# File 'lib/webmidi/port/map.rb', line 8

def initialize(ports = [], mutable: true)
  @ports = {}
  @mutable = mutable
  @mutex = Mutex.new
  ports.each { |port| @ports[port.id] = port }
end

Instance Method Details

#[](id_or_name) ⇒ Object



15
16
17
18
19
# File 'lib/webmidi/port/map.rb', line 15

def [](id_or_name)
  @mutex.synchronize do
    @ports[id_or_name] || @ports.values.find { |p| p.name == id_or_name }
  end
end

#add(port) ⇒ Object



29
30
31
32
33
# File 'lib/webmidi/port/map.rb', line 29

def add(port)
  ensure_mutable!
  @mutex.synchronize { @ports[port.id] = port }
  self
end

#each(&block) ⇒ Object



21
22
23
# File 'lib/webmidi/port/map.rb', line 21

def each(&block)
  to_a.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/webmidi/port/map.rb', line 54

def empty?
  @mutex.synchronize { @ports.empty? }
end

#remove(port_or_id) ⇒ Object



35
36
37
38
39
40
# File 'lib/webmidi/port/map.rb', line 35

def remove(port_or_id)
  ensure_mutable!
  id = port_or_id.is_a?(String) ? port_or_id : port_or_id.id
  @mutex.synchronize { @ports.delete(id) }
  self
end

#sizeObject



25
26
27
# File 'lib/webmidi/port/map.rb', line 25

def size
  @mutex.synchronize { @ports.size }
end

#snapshotObject



50
51
52
# File 'lib/webmidi/port/map.rb', line 50

def snapshot
  self.class.new(to_a, mutable: false)
end

#to_aObject



42
43
44
# File 'lib/webmidi/port/map.rb', line 42

def to_a
  @mutex.synchronize { @ports.values.dup }
end

#to_hObject



46
47
48
# File 'lib/webmidi/port/map.rb', line 46

def to_h
  @mutex.synchronize { @ports.dup }
end