Class: NOTAM::Item
- Inherits:
-
Object
- Object
- NOTAM::Item
- Defined in:
- lib/notam/item.rb
Overview
Items are the building blocks of a NOTAM message. They usually consist of only one line of plain text each, however, D and E items may span over multiple lines of plain text.
Constant Summary collapse
- RE =
/[QA-G]\)\s/.freeze
- ID_RE =
/(?<id_series>[A-Z])(?<id_number>\d{4})\/(?<id_year>\d{2})/.freeze
- ICAO_RE =
/[A-Z]{4}/.freeze
- TIME_RE =
/(?:\d{2})(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01])(?:[01]\d|[2][0-4])(?:[0-5]\d)/.freeze
Instance Attribute Summary collapse
-
#captures ⇒ MatchData
readonly
Captures from the default item regexp
RE
. -
#data ⇒ Hash
readonly
Parsed NOTAM message payload.
-
#text ⇒ String
readonly
Raw NOTAM item text.
Class Method Summary collapse
-
.type(text) ⇒ String
Analyses the raw NOTAM item text and detect its type.
Instance Method Summary collapse
-
#fail!(message = nil) ⇒ Object
Raise ParseError along with some debugging information.
-
#initialize(text, data: {}) ⇒ NOTAM::Header, ...
constructor
Analyses the raw NOTAM item text and initialize the corresponding item object.
- #inspect ⇒ String
-
#merge(*methods) ⇒ self
abstract
Merges the return values of the given methods into the
data
hash. -
#parse ⇒ self
abstract
Matches the raw NOTAM item text against
RE
and populates #captures. -
#type ⇒ Symbol
Type of the item.
Constructor Details
#initialize(text, data: {}) ⇒ NOTAM::Header, ...
Some NOTAM items (most notably D) depend on previous items for meaningful parsing and may fail if this information is not made available by passing the NOTAM message payload parsed so far as data
.
Analyses the raw NOTAM item text and initialize the corresponding item object.
47 48 49 |
# File 'lib/notam/item.rb', line 47 def initialize(text, data: {}) @text, @data = text.strip, data end |
Instance Attribute Details
#captures ⇒ MatchData (readonly)
Captures from the default item regexp RE
24 25 26 |
# File 'lib/notam/item.rb', line 24 def captures @captures end |
#data ⇒ Hash (readonly)
Parsed NOTAM message payload
29 30 31 |
# File 'lib/notam/item.rb', line 29 def data @data end |
#text ⇒ String (readonly)
Raw NOTAM item text
19 20 21 |
# File 'lib/notam/item.rb', line 19 def text @text end |
Class Method Details
.type(text) ⇒ String
Analyses the raw NOTAM item text and detect its type.
70 71 72 73 74 75 76 77 |
# File 'lib/notam/item.rb', line 70 def type(text) case text.strip when /\A([A-GQ])\)/ then $1 when NOTAM::Header::RE then 'Header' when NOTAM::Footer::RE then 'Footer' else fail(NOTAM::ParseError, 'item not recognized') end.to_sym end |
Instance Method Details
#fail!(message = nil) ⇒ Object
Raise ParseError along with some debugging information.
130 131 132 |
# File 'lib/notam/item.rb', line 130 def fail!(=nil) fail ParseError.new([, text].compact.join(': '), item: self) end |
#inspect ⇒ String
135 136 137 |
# File 'lib/notam/item.rb', line 135 def inspect %Q(#<#{self.class} "#{truncated_text}">) end |
#merge(*methods) ⇒ self
Must be extended in subclasses.
Merges the return values of the given methods into the data
hash.
112 113 114 115 116 117 |
# File 'lib/notam/item.rb', line 112 def merge(*methods) fail 'nothing to merge' unless methods.any? methods.each { @data[_1] = send(_1) } @data.compact! self end |
#parse ⇒ self
May be extended or overwritten in subclasses, but must always return self
!
Matches the raw NOTAM item text against RE
and populates #captures.
92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/notam/item.rb', line 92 def parse if match_data = self.class::RE.match(text) begin @captures = match_data.named_captures self rescue fail! "invalid #{self.class.to_s.split('::').last} item" end else fail! 'text does not match regexp' end end |
#type ⇒ Symbol
Type of the item
122 123 124 |
# File 'lib/notam/item.rb', line 122 def type self.class.to_s[7..].to_sym end |