Module: MIDICommunications::Device::ClassMethods

Includes:
Enumerable
Included in:
Input, Output
Defined in:
lib/midi-communications/device.rb

Overview

Methods that are shared by both Input and Output classes

Instance Method Summary collapse

Instance Method Details

#at(index) ⇒ Input, Output Also known as: []

Select the device at the given index

Parameters:

  • index (Integer)

Returns:



78
79
80
# File 'lib/midi-communications/device.rb', line 78

def at(index)
  all[index]
end

#each(&block) ⇒ Object

Iterate over all devices of this direction (eg Input, Output)



9
10
11
# File 'lib/midi-communications/device.rb', line 9

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

#find_by_name(name) ⇒ Input, Output

Shortcut to select a device by its name

Parameters:

  • name (String, Symbol)

Returns:



26
27
28
# File 'lib/midi-communications/device.rb', line 26

def find_by_name(name)
  all.find { |device| name.to_s == device.name }
end

#first(&block) ⇒ Input, Output

Select the first device and enable it

Returns:



52
53
54
# File 'lib/midi-communications/device.rb', line 52

def first(&block)
  use_device(all.first, &block)
end

#gets(&block) ⇒ Object

Streamlined console prompt that asks the user to select a device When their input is received, the device is selected and enabled



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/midi-communications/device.rb', line 32

def gets(&block)
  device = nil
  direction = get_direction
  puts ''
  puts "Select a MIDI #{direction}..."
  while device.nil?
    list
    print '> '
    selection = $stdin.gets.chomp
    if selection != ''
      selection = Integer(selection) rescue nil
      device = all.find { |d| d.id == selection } unless selection.nil?
    end
  end
  device.open(&block)
  device
end

#last(&block) ⇒ Input, Output

Select the last device and enable it

Returns:



58
59
60
# File 'lib/midi-communications/device.rb', line 58

def last(&block)
  use_device(all.last, &block)
end

#listArray<String>

Prints ids and names of each device to the console

Returns:

  • (Array<String>)


15
16
17
18
19
20
21
# File 'lib/midi-communications/device.rb', line 15

def list
  all.map do |device|
    name = "#{device.id}) #{device.display_name}"
    puts(name)
    name
  end
end

#use(index, &block) ⇒ Input, Output Also known as: open

Select the device at the given index and enable it

Parameters:

  • index (Integer)

Returns:



65
66
67
68
69
70
71
72
# File 'lib/midi-communications/device.rb', line 65

def use(index, &block)
  index = case index
          when :first then 0
          when :last then all.count - 1
          else index
          end
  use_device(at(index), &block)
end