Class: JSound::Midi::Devices::Transformer
- Inherits:
-
JSound::Midi::Device
- Object
- JSound::Midi::Device
- JSound::Midi::Devices::Transformer
- Defined in:
- lib/jsound/midi/devices/transformer.rb
Instance Attribute Summary collapse
-
#clone ⇒ Object
If true, messages will be automatically cloned when received by this device.
-
#mappings ⇒ Object
A Hash of mappings, with entries in the format {:message_attribute => lambda{|attribute| … } }.
-
#transform ⇒ Object
The transformation block: a lambda that takes a message and returns either a transformed message or an Enumerable list of messages.
Instance Method Summary collapse
-
#initialize(mappings = nil, &transform) ⇒ Transformer
constructor
A new instance of Transformer.
- #message(message) ⇒ Object
Methods inherited from JSound::Midi::Device
#<=, #>>, #close, #open, #open?, #output, #output=, #to_s, #type
Methods included from TypeFromClassName
Constructor Details
#initialize(mappings = nil, &transform) ⇒ Transformer
Returns a new instance of Transformer.
22 23 24 25 26 27 |
# File 'lib/jsound/midi/devices/transformer.rb', line 22 def initialize(mappings=nil, &transform) @clone = !!mappings.fetch(:clone, true) if mappings mappings.delete :clone if mappings @mappings = mappings @transform = transform end |
Instance Attribute Details
#clone ⇒ Object
If true, messages will be automatically cloned when received by this device. Messages are mutable, so in many cases it’s important to clone a message before changing it.
12 13 14 |
# File 'lib/jsound/midi/devices/transformer.rb', line 12 def clone @clone end |
#mappings ⇒ Object
A Hash of mappings, with entries in the format {:message_attribute => lambda{|attribute| … } }
8 9 10 |
# File 'lib/jsound/midi/devices/transformer.rb', line 8 def mappings @mappings end |
#transform ⇒ Object
The transformation block: a lambda that takes a message and returns either a transformed message or an Enumerable list of messages
16 17 18 |
# File 'lib/jsound/midi/devices/transformer.rb', line 16 def transform @transform end |
Instance Method Details
#message(message) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/jsound/midi/devices/transformer.rb', line 29 def () = .clone if @clone for attr, mapping in @mappings setter = "#{attr}=" .send setter, mapping[.send attr] if .respond_to? attr and .respond_to? setter end if @mappings if @output if @transform = @transform[] if .is_a? Enumerable .each{|m| @output.(m) } else super() if end elsif @clone or !@mappings.empty? super() end end end |