Class: Mongokit::CsvIO
- Inherits:
-
Object
- Object
- Mongokit::CsvIO
- Extended by:
- Forwardable
- Defined in:
- lib/mongokit/csv_transformer/csv_io.rb
Instance Attribute Summary collapse
-
#column_mapping ⇒ Object
readonly
Returns the value of attribute column_mapping.
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#csv ⇒ Object
readonly
Returns the value of attribute csv.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #build_column_mapping(columns) ⇒ Object
-
#initialize(operation, file, columns, options = {}) ⇒ CsvIO
constructor
A new instance of CsvIO.
- #to_attrs(row, block) ⇒ Object
- #to_row(record, block) ⇒ Object
- #write_headers ⇒ Object
Constructor Details
#initialize(operation, file, columns, options = {}) ⇒ CsvIO
Returns a new instance of CsvIO.
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/mongokit/csv_transformer/csv_io.rb', line 11 def initialize(operation, file, columns, = {}) = @column_mapping = build_column_mapping(columns) @columns = @column_mapping.keys.map(&:to_sym) if operation == :write @csv = CSV.open(file, 'wb') write_headers else @csv = CSV.open(file) @csv.readline if [:headers] end end |
Instance Attribute Details
#column_mapping ⇒ Object (readonly)
Returns the value of attribute column_mapping.
6 7 8 |
# File 'lib/mongokit/csv_transformer/csv_io.rb', line 6 def column_mapping @column_mapping end |
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
6 7 8 |
# File 'lib/mongokit/csv_transformer/csv_io.rb', line 6 def columns @columns end |
#csv ⇒ Object (readonly)
Returns the value of attribute csv.
6 7 8 |
# File 'lib/mongokit/csv_transformer/csv_io.rb', line 6 def csv @csv end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/mongokit/csv_transformer/csv_io.rb', line 6 def end |
Instance Method Details
#build_column_mapping(columns) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/mongokit/csv_transformer/csv_io.rb', line 25 def build_column_mapping(columns) if columns.is_a?(Array) columns.zip((0..columns.length).to_a).to_h.reject{|k| k.nil? } else columns.sort_by{|_, pos| pos }.to_h end end |
#to_attrs(row, block) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/mongokit/csv_transformer/csv_io.rb', line 43 def to_attrs(row, block) attrs = column_mapping.inject({}) do |r, (f, pos)| r[f] = row[pos] r end if block return false if block.call(row, attrs) == false end attrs end |
#to_row(record, block) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/mongokit/csv_transformer/csv_io.rb', line 56 def to_row(record, block) row = CSV::Row.new(columns, columns.map {|f| record[f] }) if block return false if block.call(row, record) == false end row end |
#write_headers ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/mongokit/csv_transformer/csv_io.rb', line 33 def write_headers return if [:headers].nil? if [:headers] === true csv << column_mapping.map { |f, _| f.to_s.titleize } else csv << [:headers] end end |