Class: NMEAPlus::SourceDecoder
- Inherits:
-
Object
- Object
- NMEAPlus::SourceDecoder
- Defined in:
- lib/nmea_plus.rb
Overview
The NMEA source decoder wraps an IO object, converting each_line functionality to #each_message or #each_complete_message
Instance Attribute Summary collapse
-
#throw_on_parse_fail ⇒ bool
False by default.
-
#throw_on_unrecognized_type ⇒ bool
False by default.
Instance Method Summary collapse
-
#each_complete_message {|NMEAPlus::Message| ... } ⇒ void
Executes the block for every valid NMEA message in the source stream, attempting to group multipart messages into message chains.
-
#each_message {|NMEAPlus::Message| ... } ⇒ void
Executes the block for every valid NMEA message in the source stream.
-
#initialize(line_reader) ⇒ SourceDecoder
constructor
A new instance of SourceDecoder.
Constructor Details
#initialize(line_reader) ⇒ SourceDecoder
Returns a new instance of SourceDecoder.
24 25 26 27 28 29 30 31 |
# File 'lib/nmea_plus.rb', line 24 def initialize(line_reader) unless line_reader.respond_to? :each_line fail ArgumentError, "line_reader must inherit from type IO (or implement each_line)" end @throw_on_parse_fail = false @source = line_reader @decoder = NMEAPlus::Decoder.new end |
Instance Attribute Details
#throw_on_parse_fail ⇒ bool
False by default.
17 18 19 |
# File 'lib/nmea_plus.rb', line 17 def throw_on_parse_fail @throw_on_parse_fail end |
#throw_on_unrecognized_type ⇒ bool
False by default. Typically for development.
21 22 23 |
# File 'lib/nmea_plus.rb', line 21 def throw_on_unrecognized_type @throw_on_unrecognized_type end |
Instance Method Details
#each_complete_message {|NMEAPlus::Message| ... } ⇒ void
This method returns an undefined value.
Executes the block for every valid NMEA message in the source stream, attempting to group multipart messages into message chains.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/nmea_plus.rb', line 56 def partials = {} do |msg| slot = msg.data_type if partials[slot].nil? partials[slot] = msg else # the message was already in there if 1 != (msg. - partials[slot].) # error! just overwrite what was there partials[slot] = msg else partials[slot].(msg) end end # take action if we've completed the chain maybe_full = partials[slot] if maybe_full. partials[slot] = nil yield maybe_full end end end |
#each_message {|NMEAPlus::Message| ... } ⇒ void
This method returns an undefined value.
Executes the block for every valid NMEA message in the source stream
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/nmea_plus.rb', line 36 def @source.each_line do |line| if @throw_on_parse_fail yield @decoder.parse(line) else got_error = false begin y = @decoder.parse(line) rescue got_error = true end yield y unless got_error end end end |