Module: UniMIDI::CongruousApiAdapter::Device::ClassMethods

Includes:
Enumerable
Included in:
UniMIDI::CongruousApiDevice, UniMIDI::CongruousApiInput, UniMIDI::CongruousApiOutput
Defined in:
lib/unimidi/congruous_api_adapter.rb

Instance Method Summary collapse

Instance Method Details

#[](index) ⇒ Object

returns the device at index



114
115
116
# File 'lib/unimidi/congruous_api_adapter.rb', line 114

def [](index)
  all[index]
end

#allObject

returns all devices in an array



98
99
100
# File 'lib/unimidi/congruous_api_adapter.rb', line 98

def all
  all_by_type.values.flatten
end

#all_by_typeObject

returns all devices as a hash as such

{ :input => [input devices], :output => [output devices] }


120
121
122
123
# File 'lib/unimidi/congruous_api_adapter.rb', line 120

def all_by_type
  ensure_initialized
  @devices
end

#defer_to(klass) ⇒ Object



125
126
127
128
# File 'lib/unimidi/congruous_api_adapter.rb', line 125

def defer_to(klass)
  @deference ||= {}
  @deference[self] = klass
end

#device_class(klass) ⇒ Object



130
131
132
# File 'lib/unimidi/congruous_api_adapter.rb', line 130

def device_class(klass)
  @device_class = klass
end

#each(&block) ⇒ Object



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

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

#find_by_name(name) ⇒ Object

Shortcut to get a device by its name



65
66
67
# File 'lib/unimidi/congruous_api_adapter.rb', line 65

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

#first(&block) ⇒ Object

returns the first device for this class



88
89
90
# File 'lib/unimidi/congruous_api_adapter.rb', line 88

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

#gets(&block) ⇒ Object

streamlined console prompt that asks the user to select a device



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/unimidi/congruous_api_adapter.rb', line 70

def gets(&block)
  device = nil
  class_name = self.name.split("::").last.downcase
  puts ""
  puts "Select a MIDI #{class_name}..."
  while device.nil?
    list
    print "> "
    selection = $stdin.gets.chomp
    if selection != ""
      selection = Integer(selection) rescue nil
      device = all.find { |d| d.id == selection }
    end
  end
  device.open(&block)
end

#input_class(klass) ⇒ Object



134
135
136
# File 'lib/unimidi/congruous_api_adapter.rb', line 134

def input_class(klass)
  @input_class = klass
end

#last(&block) ⇒ Object

returns the last device for this class



93
94
95
# File 'lib/unimidi/congruous_api_adapter.rb', line 93

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

#listObject

Prints ids and names of each device to the console



60
61
62
# File 'lib/unimidi/congruous_api_adapter.rb', line 60

def list
  all.each { |device| puts(device.pretty_name) }
end

#output_class(klass) ⇒ Object



138
139
140
# File 'lib/unimidi/congruous_api_adapter.rb', line 138

def output_class(klass)
  @output_class = klass
end

#populateObject Also known as: refresh



142
143
144
145
146
147
148
# File 'lib/unimidi/congruous_api_adapter.rb', line 142

def populate
  klass = @deference[self].respond_to?(:all_by_type) ? @deference[self] : @device_class
  @devices = {
    :input => klass.all_by_type[:input].map { |d| @input_class.new(d) },
    :output => klass.all_by_type[:output].map { |d| @output_class.new(d) }
  }          
end

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

returns the device at index and opens it



103
104
105
106
107
108
109
110
# File 'lib/unimidi/congruous_api_adapter.rb', line 103

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