class Dif::Reader
Constants
- ALLOWED_COMMANDS
Attributes
column_count[R]
csv[R]
lines[R]
rows_count[R]
Public Class Methods
new(file, encoding="IBM850")
click to toggle source
# File lib/dif.rb, line 34 def initialize(file, encoding="IBM850") @lines = ::File.read(file, :external_encoding => encoding, :internal_encoding => "UTF-8").lines @csv = CSV.new("", col_sep: "\t") @line_buffer=Array.new fix_lines set_rows set_columns @lines.slice! 0..data_section_start_at_line read_data end
Public Instance Methods
export_csv()
click to toggle source
# File lib/dif.rb, line 45 def export_csv @csv.string end
Private Instance Methods
data_section_start_at_line()
click to toggle source
# File lib/dif.rb, line 63 def data_section_start_at_line @data_section_start_at_line ||= lines.index("DATA") end
fix_lines()
click to toggle source
# File lib/dif/line_helpers.rb, line 6 def fix_lines @lines.map! do |line| line.chomp! end end
process_command(index)
click to toggle source
# File lib/dif.rb, line 87 def process_command(index) command = lines[index.next] raise "Command not in allowed list" if !ALLOWED_COMMANDS.include? command csv << @line_buffer if !@line_buffer.empty? case command when "BOT" @line_buffer.clear when "EOD" return end end
read_data()
click to toggle source
# File lib/dif.rb, line 67 def read_data @lines.slice! 0..lines.index("BOT") # remove everything up to first BOT lines.each_with_index do |line,index| #iterate over data section # only looking for lines in the form of -1,15 (two digits with a comma between) line_eval = /(?<command>-*\d),(?<value>\d+)/.match(line) next if not line_eval case line_eval[:command] when "-1" process_command(index) when "0" #value @line_buffer << line_eval[:value].to_i when "1" @line_buffer << lines[index.next].sub(/^"/,"").sub(/"$/,"") end end end
set_columns()
click to toggle source
# File lib/dif.rb, line 57 def set_columns # find the line with TUPLES, move to the next, split, get last item, convert to integer and save in instance variable. tuple_line = lines.index("TUPLES") @column_count = lines[tuple_line.next].split(",").last.to_i end
set_rows()
click to toggle source
# File lib/dif.rb, line 51 def set_rows # find the line with VECTORS, move to the next, split, get last item, convert to integer and save in instance variable. vector_line =lines.index("VECTORS") @rows_count = lines[vector_line.next].split(",").last.to_i end