Class: SimpleCSV

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/SimpleCSV.rb,
lib/SimpleCSV/File.rb,
lib/SimpleCSV/String.rb

Direct Known Subclasses

File, String

Defined Under Namespace

Classes: File, String

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, *args) ⇒ SimpleCSV

Returns a new instance of SimpleCSV.



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# 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

Instance Attribute Details

#as_arrayObject

Returns the value of attribute as_array.



164
165
166
# File 'lib/SimpleCSV.rb', line 164

def as_array
  @as_array
end

#header_rowObject

Returns the value of attribute header_row.



164
165
166
# File 'lib/SimpleCSV.rb', line 164

def header_row
  @header_row
end

#modeObject

Returns the value of attribute mode.



164
165
166
# File 'lib/SimpleCSV.rb', line 164

def mode
  @mode
end

#quoteObject

Returns the value of attribute quote.



164
165
166
# File 'lib/SimpleCSV.rb', line 164

def quote
  @quote
end

#row_separatorObject

Returns the value of attribute row_separator.



164
165
166
# File 'lib/SimpleCSV.rb', line 164

def row_separator
  @row_separator
end

#rowsObject

Returns the value of attribute rows.



164
165
166
# File 'lib/SimpleCSV.rb', line 164

def rows
  @rows
end

#selected_columnsObject

Returns the value of attribute selected_columns.



164
165
166
# File 'lib/SimpleCSV.rb', line 164

def selected_columns
  @selected_columns
end

Class Method Details

.attributes(source, *args) ⇒ Object



144
145
146
# File 'lib/SimpleCSV.rb', line 144

def attributes(source, *args)
  new(source, *args).attributes
end

.collect(source, *args, &block) ⇒ Object Also known as: map



88
89
90
91
92
# 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

.columns(source, *args) ⇒ Object



148
149
150
# File 'lib/SimpleCSV.rb', line 148

def columns(source, *args)
  new(source, *args).columns
end

.detect(source, *args, &block) ⇒ Object Also known as: find



108
109
110
# File 'lib/SimpleCSV.rb', line 108

def detect(source, *args, &block)
  each(source, *args){|row| return row if block.call(row)}
end

.each(source, *args, &block) ⇒ Object



83
84
85
# File 'lib/SimpleCSV.rb', line 83

def each(source, *args, &block)
  new(source, *args).each(&block)
end

.first_row(source, *args) ⇒ Object



140
141
142
# File 'lib/SimpleCSV.rb', line 140

def first_row(source, *args)
  new(source, *args).first_row
end

.foreachObject



86
87
88
# File 'lib/SimpleCSV.rb', line 86

def each(source, *args, &block)
  new(source, *args).each(&block)
end

.header_row(source, *args) ⇒ Object



136
137
138
# File 'lib/SimpleCSV.rb', line 136

def header_row(source, *args)
  new(source, *args).header_row
end

.open(source, *args, &block) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# 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) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/SimpleCSV.rb', line 122

def parse(source, *args, &block)
  if block
    each(source, *args, &block)
  else
    read(source, *args)
  end
end

.parse_csvObject



129
130
131
132
133
134
135
# File 'lib/SimpleCSV.rb', line 129

def parse(source, *args, &block)
  if block
    each(source, *args, &block)
  else
    read(source, *args)
  end
end

.parse_line(raw_row, *args) ⇒ Object

For FasterCSV compatibility.



152
153
154
155
156
157
158
# 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) ⇒ Object



113
114
115
116
117
118
119
# 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

.read_csvObject



120
121
122
123
124
125
126
# File 'lib/SimpleCSV.rb', line 120

def read(source, *args, &block)
  if block
    parse(source, *args, &block)
  else
    new(source, *args).read_csv
  end
end

.reject(source, *args, &block) ⇒ Object



102
103
104
105
106
# 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) ⇒ Object Also known as: find_all



95
96
97
98
99
# 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

.source_type(source) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/SimpleCSV.rb', line 61

def source_type(source)
  if ::File.exist?(source)
    SimpleCSV::File
  else
    SimpleCSV::String
  end
end

.write(source, *args) ⇒ Object



131
132
133
# File 'lib/SimpleCSV.rb', line 131

def write(source, *args)
  new(source, *args).write_csv
end

.write_csvObject



134
135
136
# File 'lib/SimpleCSV.rb', line 134

def write(source, *args)
  new(source, *args).write_csv
end

Instance Method Details

#attributesObject



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# 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) ⇒ Object



354
355
356
# File 'lib/SimpleCSV.rb', line 354

def attributes=(attributes)
  @attributes = attributes
end

#closeObject



190
191
192
# File 'lib/SimpleCSV.rb', line 190

def close
  @source.close
end

#columnsObject



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# 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) ⇒ Object



243
244
245
246
247
248
249
250
251
252
# 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) ⇒ Object Also known as: each_row



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# 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

#first_rowObject Also known as: first_row?



362
363
364
365
366
367
# File 'lib/SimpleCSV.rb', line 362

def first_row
  @source.rewind
  return_value = @source.gets(@row_separator)
  @source.rewind
  return_value
end

#header_row?Boolean

Returns:

  • (Boolean)


358
359
360
# File 'lib/SimpleCSV.rb', line 358

def header_row?
  @header_row
end

#parse(*selected_columns, &block) ⇒ Object Also known as: parse_csv



216
217
218
219
220
221
222
# File 'lib/SimpleCSV.rb', line 216

def parse(*selected_columns, &block)
  if block
    each(*selected_columns, &block)
  else
    read(*selected_columns)
  end
end

#parse_row(raw_row, *selected_columns) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# 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) ⇒ Object Also known as: read_csv



194
195
196
197
198
199
200
201
202
203
# 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

#read_headerObject Also known as: read_csv_header



206
207
208
209
210
211
212
213
# File 'lib/SimpleCSV.rb', line 206

def read_header
  columns
  if header_row?
    (@source.rewind; @source.gets(@row_separator))
  else
    @source.rewind
  end
end

#to_aObject



370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# 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) ⇒ Object Also known as: write_csv



283
284
285
286
# 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

#write_header(*selected_columns) ⇒ Object Also known as: write_csv_header



289
290
291
292
293
294
295
296
# 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

#write_row(row, *selected_columns) ⇒ Object Also known as: write_csv_row



299
300
301
302
303
304
305
306
307
308
309
310
# 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