Module: UniMIDI::Device::ClassMethods

Includes:
Enumerable
Included in:
Input, Output
Defined in:
lib/unimidi/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



84
85
86
# File 'lib/unimidi/device.rb', line 84

def at(index)
  all[index]
end

#each(&block) ⇒ Object

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



11
12
13
# File 'lib/unimidi/device.rb', line 11

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

#find_by_name(name) ⇒ Input, Output

Shortcut to select a device by its name



28
29
30
# File 'lib/unimidi/device.rb', line 28

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



58
59
60
# File 'lib/unimidi/device.rb', line 58

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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/unimidi/device.rb', line 34

def gets(&block)
  device = nil
  direction = get_direction
  puts ''
  puts "Select a MIDI #{direction}..."
  while device.nil?
    list
    print '> '
    selection = $stdin.gets.chomp
    next unless selection != ''

    selection = begin
      Integer(selection)
    rescue StandardError
      nil
    end
    device = all.find { |d| d.id == selection } unless selection.nil?
  end
  device.open(&block)
  device
end

#last(&block) ⇒ Input, Output

Select the last device and enable it



64
65
66
# File 'lib/unimidi/device.rb', line 64

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

#listArray<String>

Prints ids and names of each device to the console



17
18
19
20
21
22
23
# File 'lib/unimidi/device.rb', line 17

def list
  all.map do |device|
    name = device.pretty_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



71
72
73
74
75
76
77
78
# File 'lib/unimidi/device.rb', line 71

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