Class: Insulin::OnTrack::CsvFile
- Inherits:
-
Object
- Object
- Insulin::OnTrack::CsvFile
- Defined in:
- lib/insulin/on_track/csv_file.rb
Overview
This class represents a CSV file as exported by OnTrack
Instance Attribute Summary collapse
-
#events ⇒ Object
readonly
Returns the value of attribute events.
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#lines ⇒ Object
readonly
Returns the value of attribute lines.
Instance Method Summary collapse
-
#create_events ⇒ Object
Turn the lines into Events.
-
#initialize(csv_path) ⇒ CsvFile
constructor
Take the path to the CSV, open it, process it.
-
#read_lines ⇒ Object
Read the lines.
-
#save_events(mongo) ⇒ Object
Save the events to Mongo.
Constructor Details
#initialize(csv_path) ⇒ CsvFile
Take the path to the CSV, open it, process it
12 13 14 15 16 17 |
# File 'lib/insulin/on_track/csv_file.rb', line 12 def initialize csv_path @csv_path = csv_path @file = File.new @csv_path self.read_lines self.create_events end |
Instance Attribute Details
#events ⇒ Object (readonly)
Returns the value of attribute events.
9 10 11 |
# File 'lib/insulin/on_track/csv_file.rb', line 9 def events @events end |
#file ⇒ Object (readonly)
Returns the value of attribute file.
9 10 11 |
# File 'lib/insulin/on_track/csv_file.rb', line 9 def file @file end |
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
9 10 11 |
# File 'lib/insulin/on_track/csv_file.rb', line 9 def lines @lines end |
Instance Method Details
#create_events ⇒ Object
Turn the lines into Events
46 47 48 49 50 51 52 |
# File 'lib/insulin/on_track/csv_file.rb', line 46 def create_events @events = [] @lines.each do |l| e = Event.new l @events << e end end |
#read_lines ⇒ Object
Read the lines
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/insulin/on_track/csv_file.rb', line 20 def read_lines @lines = [] # Where an event has more than one note, a 'line' will contain '\n's l = "" while line = @file.gets # Stuff the line in to 'l' l << line # A '"' at the end of the line closes the event if line[-2] == '"' # Create a CsvLine, stripping the final '\n' o = CsvLine.new l[0..-2] @lines << o # Reset the placeholder l = "" end end # Sort the list by the serial numbers (required for a future feature) @lines.sort_by! {|o| o["serial"]} end |
#save_events(mongo) ⇒ Object
Save the events to Mongo
55 56 57 58 59 60 61 |
# File 'lib/insulin/on_track/csv_file.rb', line 55 def save_events mongo # print "saving %d events to mongo... " % @events.count @events.each do |e| e.save mongo end # puts "done" end |