Class: Ethel::Targets::CSV
- Inherits:
-
Ethel::Target
- Object
- Ethel::Target
- Ethel::Targets::CSV
- Defined in:
- lib/ethel/targets/csv.rb
Instance Method Summary collapse
- #add_field(field) ⇒ Object
- #add_row(row) ⇒ Object
- #data ⇒ Object
- #flush ⇒ Object
-
#initialize(options) ⇒ CSV
constructor
A new instance of CSV.
Methods inherited from Ethel::Target
Constructor Details
#initialize(options) ⇒ CSV
Returns a new instance of CSV.
4 5 6 7 8 9 10 |
# File 'lib/ethel/targets/csv.rb', line 4 def initialize() super @options = @fields = [] @rows = [] end |
Instance Method Details
#add_field(field) ⇒ Object
12 13 14 |
# File 'lib/ethel/targets/csv.rb', line 12 def add_field(field) @fields << field end |
#add_row(row) ⇒ Object
16 17 18 |
# File 'lib/ethel/targets/csv.rb', line 16 def add_row(row) @rows << row end |
#data ⇒ Object
39 40 41 42 43 |
# File 'lib/ethel/targets/csv.rb', line 39 def data if @options[:string] @data end end |
#flush ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/ethel/targets/csv.rb', line 20 def flush headers = @fields.collect(&:name) = { :headers => headers, :write_headers => true } csv = if @options[:file] ::CSV.open(@options[:file], 'wb', ) elsif @options[:string] @data = "" ::CSV.new(@data, ) end @rows.each do |row| csv << row.values_at(*headers) end csv.close end |