Class: Ethel::Sources::CSV

Inherits:
Ethel::Source show all
Defined in:
lib/ethel/sources/csv.rb

Instance Method Summary collapse

Methods inherited from Ethel::Source

#all, #field_names, #fields

Constructor Details

#initialize(options = {}) ⇒ CSV

Returns a new instance of CSV.



4
5
6
7
8
9
10
# File 'lib/ethel/sources/csv.rb', line 4

def initialize(options = {})
  if options[:string]
    @data = ::CSV.parse(options[:string], :headers => true)
  elsif options[:file]
    @data = ::CSV.read(options[:file], :headers => true)
  end
end

Instance Method Details

#eachObject



22
23
24
25
26
# File 'lib/ethel/sources/csv.rb', line 22

def each
  @data.each do |row|
    yield row.to_hash
  end
end

#schemaObject



12
13
14
15
16
17
18
19
20
# File 'lib/ethel/sources/csv.rb', line 12

def schema
  if @schema.nil?
    @schema = []
    @data.headers.each do |name|
      @schema << [name, {:type => :string}]
    end
  end
  @schema
end