Module: UniMIDI::Device::ClassMethods
Overview
Methods that are shared by both Input and Output classes
Instance Method Summary collapse
-
#at(index) ⇒ Input, Output
(also: #[])
Select the device at the given index.
-
#each(&block) ⇒ Object
Iterate over all devices of this direction (eg Input, Output).
-
#find_by_name(name) ⇒ Input, Output
Shortcut to select a device by its name.
-
#first(&block) ⇒ Input, Output
Select the first device and enable it.
-
#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.
-
#last(&block) ⇒ Input, Output
Select the last device and enable it.
-
#list ⇒ Array<String>
Prints ids and names of each device to the console.
-
#use(index, &block) ⇒ Input, Output
(also: #open)
Select the device at the given index and enable it.
Instance Method Details
#at(index) ⇒ Input, Output Also known as: []
Select the device at the given index
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
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
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
61 62 63 |
# File 'lib/unimidi/device.rb', line 61 def last(&block) use_device(all.last, &block) end |
#list ⇒ Array<String>
Prints ids and names of each device to the console
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
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 |