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

Parameters:

  • index (Integer)

Returns:


81
82
83
# File 'lib/unimidi/device.rb', line 81

def at(index)
  all[index]
end

#each(&block) ⇒ Object

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


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

def each(&block)
  all.each { |device| yield(device) }
end

#find_by_name(name) ⇒ Input, Output

Shortcut to select a device by its name

Parameters:

  • name (String, Symbol)

Returns:


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

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:


55
56
57
# File 'lib/unimidi/device.rb', line 55

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


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

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:


61
62
63
# File 'lib/unimidi/device.rb', line 61

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

#listArray<String>

Prints ids and names of each device to the console

Returns:

  • (Array<String>)

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

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

Parameters:

  • index (Integer)

Returns:


68
69
70
71
72
73
74
75
# File 'lib/unimidi/device.rb', line 68

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