class SimpleCSV
Attributes
as_array[RW]
header_row[RW]
mode[RW]
quote[RW]
row_separator[RW]
rows[RW]
selected_columns[RW]
Public Class Methods
attributes(source, *args)
click to toggle source
# File lib/SimpleCSV.rb, line 144 def attributes(source, *args) new(source, *args).attributes end
collect(source, *args, &block)
click to toggle source
# File lib/SimpleCSV.rb, line 88 def collect(source, *args, &block) new_collection = [] each(source, *args){|row| new_collection << block.call(row)} new_collection end
Also aliased as: map
columns(source, *args)
click to toggle source
# File lib/SimpleCSV.rb, line 148 def columns(source, *args) new(source, *args).columns end
detect(source, *args, &block)
click to toggle source
# File lib/SimpleCSV.rb, line 108 def detect(source, *args, &block) each(source, *args){|row| return row if block.call(row)} end
Also aliased as: find
each(source, *args, &block)
click to toggle source
# File lib/SimpleCSV.rb, line 83 def each(source, *args, &block) new(source, *args).each(&block) end
Also aliased as: foreach
first_row(source, *args)
click to toggle source
# File lib/SimpleCSV.rb, line 140 def first_row(source, *args) new(source, *args).first_row end
header_row(source, *args)
click to toggle source
# File lib/SimpleCSV.rb, line 136 def header_row(source, *args) new(source, *args).header_row end
new(source, *args)
click to toggle source
# File lib/SimpleCSV.rb, line 166 def initialize(source, *args) @source = ( if source.is_a?(::String) SimpleCSV.source_type(source).new(source, *args).source else source end ) options = args.extract_options! @header_row = options[:header_row] || options[:headers] || options[:header] || false @mode = options[:mode] || 'r' @quote = options[:quote] || nil @row_separator = options[:row_separator] || options[:row_sep] || "\n" @column_separator = options[:column_separator] || options[:col_sep] || ',' @selected_columns = options[:selected_columns] @as_array = options[:as_array] || false if options[:columns] self.columns = options[:columns] else self.columns end @rows = [] end
open(source, *args) { |csv_file| ... }
click to toggle source
# File lib/SimpleCSV.rb, line 69 def open(source, *args, &block) @csv_file = new(source, *args) if block begin yield @csv_file @csv_file ensure @csv_file.close end else @csv_file end end
parse(source, *args, &block)
click to toggle source
# File lib/SimpleCSV.rb, line 122 def parse(source, *args, &block) if block each(source, *args, &block) else read(source, *args) end end
Also aliased as: parse_csv
parse_line(raw_row, *args)
click to toggle source
# File lib/SimpleCSV.rb, line 152 def parse_line(raw_row, *args) # For FasterCSV compatibility. options = args.extract_options! row_separator = options[:row_separator] || options[:row_sep] || "\n" column_separator = options[:column_separator] || options[:col_sep] || ',' sc = SimpleCSV.new(raw_row, :quote => nil, :as_array => true, :row_separator => row_separator, :column_separator => column_separator) sc.parse_row(raw_row) end
read(source, *args, &block)
click to toggle source
# File lib/SimpleCSV.rb, line 113 def read(source, *args, &block) if block parse(source, *args, &block) else new(source, *args).read_csv end end
Also aliased as: read_csv
reject(source, *args, &block)
click to toggle source
# File lib/SimpleCSV.rb, line 102 def reject(source, *args, &block) new_collection = [] each(source, *args){|row| new_collection << row unless block.call(row)} new_collection end
select(source, *args, &block)
click to toggle source
# File lib/SimpleCSV.rb, line 95 def select(source, *args, &block) new_collection = [] each(source, *args){|row| new_collection << row if block.call(row)} new_collection end
Also aliased as: find_all
source_type(source)
click to toggle source
# File lib/SimpleCSV.rb, line 61 def source_type(source) if ::File.exist?(source) SimpleCSV::File else SimpleCSV::String end end
write(source, *args)
click to toggle source
# File lib/SimpleCSV.rb, line 131 def write(source, *args) new(source, *args).write_csv end
Also aliased as: write_csv
Public Instance Methods
attributes()
click to toggle source
# File lib/SimpleCSV.rb, line 335 def attributes @attributes ||= ( if columns.blank? nil else a = [] columns.each do |k,v| case v when Array v.each{|e| a << ['', e]} else a << [k, v] end end a.sort{|a,b| a[1] <=> b[1]}.collect{|a| a[0]} end ) end
attributes=(attributes)
click to toggle source
# File lib/SimpleCSV.rb, line 354 def attributes=(attributes) @attributes = attributes end
close()
click to toggle source
# File lib/SimpleCSV.rb, line 190 def close @source.close end
columns()
click to toggle source
# File lib/SimpleCSV.rb, line 225 def columns @columns ||= ( if header_row? && ['r', 'r+', 'a+'].include?(@mode) && (first_row = first_row?) columns, i = {}, -1 first_row.split_csv(@quote, @column_separator, @row_separator).each do |column_name| if column_name.empty? columns[column_name].blank? ? columns[column_name] = [i += 1] : columns[column_name] << (i += 1) else columns[column_name] = (i += 1) end end columns else nil end ) end
columns=(*column_order)
click to toggle source
# File lib/SimpleCSV.rb, line 243 def columns=(*column_order) @columns = {} column_order.flatten! if column_order[0].is_a?(Hash) column_order[0].each{|column_name, column_position| @columns[column_name.to_s] = column_position} else i = -1 column_order.each{|column| @columns[column.to_s] = (i += 1)} end end
each(*selected_columns) { |row| ... }
click to toggle source
# File lib/SimpleCSV.rb, line 313 def each(*selected_columns) selected_columns.flatten! if @rows[0] if selected_columns.empty? @rows.each{|row| yield row} else @rows.each do |row| yield selected_columns.inject({}){|hash, column_name| hash[column_name] = row[column_name]; hash} end end else if selected_columns.empty? read_csv.each{|row| yield row} else read_csv(selected_columns).each do |row| yield selected_columns.inject({}){|hash, column_name| hash[column_name] = row[column_name]; hash} end end end end
Also aliased as: each_row
first_row()
click to toggle source
# File lib/SimpleCSV.rb, line 362 def first_row @source.rewind return_value = @source.gets(@row_separator) @source.rewind return_value end
Also aliased as: first_row?
header_row?()
click to toggle source
# File lib/SimpleCSV.rb, line 358 def header_row? @header_row end
parse(*selected_columns, &block)
click to toggle source
# File lib/SimpleCSV.rb, line 216 def parse(*selected_columns, &block) if block each(*selected_columns, &block) else read(*selected_columns) end end
Also aliased as: parse_csv
parse_row(raw_row, *selected_columns)
click to toggle source
# File lib/SimpleCSV.rb, line 254 def parse_row(raw_row, *selected_columns) parsed_row = {} i = -1 if selected_columns.empty? if @columns.blank? raw_row.split_csv(@quote, @column_separator, @row_separator).each{|column_value| parsed_row[i += 1] = column_value} else raw_row.split_csv(@quote, @column_separator, @row_separator).each{|column_value| parsed_row[attributes[i += 1]] = column_value} end else selected_columns.flatten! case selected_columns[0] when Integer raw_row.split_csv(@quote, @column_separator, @row_separator).each{|column_value| parsed_row[i] = column_value unless !selected_columns.include?(i += 1)} else raw_row.split_csv(@quote, @column_separator, @row_separator).each{|column_value| parsed_row[attributes[i]] = column_value unless !selected_columns.include?(attributes[i += 1])} end end if @as_array if @columns.blank? (0..(parsed_row.size - 1)).inject([]){|a,i| a << parsed_row[i]} else attributes.collect{|attribute| parsed_row[attribute]} end else parsed_row end end
read(*selected_columns, &block)
click to toggle source
# File lib/SimpleCSV.rb, line 194 def read(*selected_columns, &block) if block parse(*selected_columns, &block) else read_header @source.each(@row_separator){|raw_row| @rows << parse_row(raw_row, *selected_columns)} (@source.rewind; @source.truncate(0)) if @mode == 'r+' @rows end end
Also aliased as: read_csv
read_header()
click to toggle source
# File lib/SimpleCSV.rb, line 206 def read_header columns if header_row? (@source.rewind; @source.gets(@row_separator)) else @source.rewind end end
Also aliased as: read_csv_header
to_a()
click to toggle source
# File lib/SimpleCSV.rb, line 370 def to_a read_csv unless @rows[0] if @as_array @rows elsif @columns.blank? result = [] @rows.each do |row| a = [] (0..(row.size - 1)).inject([]){|a,i| a << row[i]} result << a end result else @rows.collect do |row| attributes.collect{|attribute| row[attribute]} end end end
write(*selected_columns)
click to toggle source
# File lib/SimpleCSV.rb, line 283 def write(*selected_columns) write_header(*selected_columns) if header_row? each{|row| write_row(row, *selected_columns)} end
Also aliased as: write_csv
write_header(*selected_columns)
click to toggle source
# File lib/SimpleCSV.rb, line 289 def write_header(*selected_columns) selected_columns.flatten! if selected_columns.empty? write_row(attributes.to_csv) else write_row(columns.to_csv) end end
Also aliased as: write_csv_header
write_row(row, *selected_columns)
click to toggle source
# File lib/SimpleCSV.rb, line 299 def write_row(row, *selected_columns) collector = [] selected_columns.flatten! unless attributes.blank? if selected_columns.blank? attributes.each{|attribute| collector << row[attribute] unless row[attribute].nil?} else selected_columns.each{|column| collector << row[column] unless row[column].nil?} end @source.puts(collector.to_csv(@quote)) end end
Also aliased as: write_csv_row