Class: Vissen::Input::Matcher
- Inherits:
-
Object
- Object
- Vissen::Input::Matcher
- Defined in:
- lib/vissen/input/matcher.rb
Overview
The job of an input matcher is to match a raw message with a message class.
== Usage
In the following example a matcher is setup to match aftertouch messages
(on channel 0). Only data arrays of the correct length and content causes
#match?
to return true
matcher = Matcher.new(Message::Aftertouch) { |d| d[0] == 0xA0 }
matcher.match? [0xB0, 0, 0] # => false matcher.match? [0xA0, 0] # => false matcher.match? [0xA0, 0, 0] # => true
Instance Attribute Summary collapse
-
#klass ⇒ Message
readonly
The message class responsible for the messages that this matcher matches.
Instance Method Summary collapse
-
#initialize(message_klass, &proc) ⇒ Matcher
constructor
A new instance of Matcher.
-
#match(obj) {|message| ... } ⇒ false, ...
Matches either a hash or a
Message
against the internal matching rule. -
#match?(obj) ⇒ true, false
Match either a byte array or a
Message
against the rule stored in the matcher.
Constructor Details
Instance Attribute Details
#klass ⇒ Message (readonly)
Returns the message class responsible for the messages that this matcher matches.
22 23 24 |
# File 'lib/vissen/input/matcher.rb', line 22 def klass @klass end |
Instance Method Details
#match(obj) {|message| ... } ⇒ false, ...
Matches either a hash or a Message
against the internal matching rule.
If the object matches and is not a Message
object a new instance will
be created.
If a block is given the message will be yielded to it.
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/vissen/input/matcher.rb', line 65 def match(obj) data = obj.fetch :data return false if data.length < @klass::DATA_LENGTH || !@rule.call(data) = case obj when Message then obj when Hash then @klass.new data, obj.fetch(:timestamp) end return unless block_given? yield end |
#match?(obj) ⇒ true, false
Match either a byte array or a Message
against the rule stored in the
matcher.
46 47 48 49 50 51 |
# File 'lib/vissen/input/matcher.rb', line 46 def match?(obj) data = obj.is_a?(Hash) ? obj.fetch(:data) : obj.to_a return false if data.length < @klass::DATA_LENGTH @rule.call data end |