Class: Rozi::WaypointFile
- Inherits:
-
FileWrapperBase
- Object
- FileWrapperBase
- Rozi::WaypointFile
- Includes:
- Shared
- Defined in:
- lib/rozi/waypoints.rb
Overview
A thin layer above File
that handles reading and writing of waypoints to files
Instance Attribute Summary
Attributes inherited from FileWrapperBase
Writing methods collapse
-
#write(waypoints) ⇒ nil
Writes waypoints to the file.
-
#write_properties(properties) ⇒ nil
Writes waypoint file properties to the file.
-
#write_waypoint(waypoint) ⇒ nil
Writes a waypoint to the file.
Reading methods collapse
-
#each_waypoint ⇒ Object
Reads and yields all waypoints.
-
#read_properties ⇒ WaypointFileProperties
Reads the waypoint file properties.
-
#read_waypoint ⇒ Waypoint
Reads the next waypoint.
Methods included from Shared
#datum_valid?, #escape_text, #interpret_color, #unescape_text
Methods inherited from FileWrapperBase
#close, #closed?, #initialize, open, #rewind
Constructor Details
This class inherits a constructor from Rozi::FileWrapperBase
Instance Method Details
#each_waypoint ⇒ Object
Reads and yields all waypoints
189 190 191 192 193 194 195 196 197 |
# File 'lib/rozi/waypoints.rb', line 189 def each_waypoint return to_enum(:each_waypoint) unless block_given? @file.rewind loop { yield read_waypoint } rescue EOFError return nil end |
#read_properties ⇒ WaypointFileProperties
Reads the waypoint file properties
205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/rozi/waypoints.rb', line 205 def read_properties if @file.pos != 0 raise "File position must be 0 to read properties" end text = "" 4.times { text << @file.readline } parse_waypoint_file_properties text end |
#read_waypoint ⇒ Waypoint
Reads the next waypoint
223 224 225 226 227 228 229 |
# File 'lib/rozi/waypoints.rb', line 223 def read_waypoint if @file.pos == 0 read_properties end parse_waypoint @file.readline end |
#write(waypoints) ⇒ nil
Writes waypoints to the file
141 142 143 144 145 146 147 |
# File 'lib/rozi/waypoints.rb', line 141 def write(waypoints) waypoints.each { |wpt| write_waypoint wpt } nil end |
#write_properties(properties) ⇒ nil
Writes waypoint file properties to the file
The file must be empty when this method is called!
158 159 160 161 162 163 164 165 166 167 |
# File 'lib/rozi/waypoints.rb', line 158 def write_properties(properties) if @file.size > 0 raise "Can't write file properties, file is not empty" end @file.write serialize_waypoint_file_properties(properties) @file.write "\n" nil end |
#write_waypoint(waypoint) ⇒ nil
Writes a waypoint to the file
175 176 177 178 179 180 181 182 |
# File 'lib/rozi/waypoints.rb', line 175 def write_waypoint(waypoint) ensure_file_properties @file.write serialize_waypoint(waypoint) @file.write "\n" nil end |