Class: Remi::DataSource::CsvFile
- Inherits:
-
Object
- Object
- Remi::DataSource::CsvFile
show all
- Includes:
- Remi::DataSource, DataStub
- Defined in:
- lib/remi/cucumber/data_source.rb,
lib/remi/data_source/csv_file.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#feild_symbolizer
#df=
Methods included from DataStub
#empty_stub_df, #stub_df, #stub_row_array, #stub_values
Constructor Details
#initialize(fields: {}, extractor:, csv_options: {}, logger: Remi::Settings.logger) ⇒ CsvFile
Returns a new instance of CsvFile.
17
18
19
20
21
22
|
# File 'lib/remi/data_source/csv_file.rb', line 17
def initialize(fields: {}, extractor:, csv_options: {}, logger: Remi::Settings.logger)
@fields = fields
self. =
@csv_options = self.class.default_csv_options.merge(csv_options)
@logger = logger
end
|
Instance Attribute Details
#csv_options ⇒ Object
Returns the value of attribute csv_options.
26
27
28
|
# File 'lib/remi/data_source/csv_file.rb', line 26
def csv_options
@csv_options
end
|
Returns the value of attribute extractor.
25
26
27
|
# File 'lib/remi/data_source/csv_file.rb', line 25
def
@extractor
end
|
#fields ⇒ Object
Returns the value of attribute fields.
24
25
26
|
# File 'lib/remi/data_source/csv_file.rb', line 24
def fields
@fields
end
|
Class Method Details
.default_csv_options ⇒ Object
6
7
8
9
10
11
12
13
14
|
# File 'lib/remi/data_source/csv_file.rb', line 6
def self.default_csv_options
CSV::DEFAULT_OPTIONS.merge({
headers: true,
header_converters: Remi::FieldSymbolizers[:standard],
col_sep: ',',
encoding: 'UTF-8',
quote_char: '"'
})
end
|
Instance Method Details
#df ⇒ Object
74
75
76
|
# File 'lib/remi/data_source/csv_file.rb', line 74
def df
@dataframe ||= to_dataframe
end
|
32
33
34
|
# File 'lib/remi/data_source/csv_file.rb', line 32
def
Array(@extractor.).tap { |x| raise "Multiple files not supported" if x.size > 1 }
end
|
#field_symbolizer ⇒ Object
28
29
30
|
# File 'lib/remi/data_source/csv_file.rb', line 28
def field_symbolizer
self.class.default_csv_options[:header_converters]
end
|
#first_line ⇒ Object
54
55
56
57
58
59
|
# File 'lib/remi/data_source/csv_file.rb', line 54
def first_line
@first_line ||= File.open(source_filename) do |f|
f.readline.gsub(/\r/,'')
end
end
|
61
62
63
|
# File 'lib/remi/data_source/csv_file.rb', line 61
def
@headers ||= CSV.open(source_filename, 'r', source_csv_options) { |csv| csv.first }.
end
|
#source_filename ⇒ Object
Only going to support single file for now
50
51
52
|
# File 'lib/remi/data_source/csv_file.rb', line 50
def source_filename
@source_filename ||= .first
end
|
53
54
55
|
# File 'lib/remi/cucumber/data_source.rb', line 53
def
@fields.keys.join(@csv_options[:col_sep])
end
|
#stub_row_csv ⇒ Object
57
58
59
|
# File 'lib/remi/cucumber/data_source.rb', line 57
def stub_row_csv
stub_row_array.join(@csv_options[:col_sep])
end
|
#stub_tmp_file ⇒ Object
40
41
42
|
# File 'lib/remi/cucumber/data_source.rb', line 40
def stub_tmp_file
@stub_tmp_file ||= Tempfile.new('stub_tmp_file.csv').path
end
|
#to_dataframe ⇒ Object
69
70
71
72
|
# File 'lib/remi/data_source/csv_file.rb', line 69
def to_dataframe
@logger.info "Converting #{source_filename} to a dataframe"
Daru::DataFrame.from_csv source_filename, @csv_options
end
|
65
66
67
|
# File 'lib/remi/data_source/csv_file.rb', line 65
def
(fields.keys - ).empty?
end
|
#write_stub_tmp_file ⇒ Object
44
45
46
47
48
49
50
51
|
# File 'lib/remi/cucumber/data_source.rb', line 44
def write_stub_tmp_file
File.open(stub_tmp_file, "wb") do |file|
file.puts
file.puts stub_row_csv
end
stub_tmp_file
end
|