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: {}, filename_field: nil, logger: Remi::Settings.logger) ⇒ CsvFile
Returns a new instance of CsvFile.
20
21
22
23
24
25
26
|
# File 'lib/remi/data_source/csv_file.rb', line 20
def initialize(fields: {}, extractor:, csv_options: {}, filename_field: nil, logger: Remi::Settings.logger)
@fields = fields
self. =
@csv_options = self.class.default_csv_options.merge(csv_options)
@filename_field = filename_field
@logger = logger
end
|
Instance Attribute Details
#csv_options ⇒ Object
Returns the value of attribute csv_options.
30
31
32
|
# File 'lib/remi/data_source/csv_file.rb', line 30
def csv_options
@csv_options
end
|
Returns the value of attribute extractor.
29
30
31
|
# File 'lib/remi/data_source/csv_file.rb', line 29
def
@extractor
end
|
#fields ⇒ Object
Returns the value of attribute fields.
28
29
30
|
# File 'lib/remi/data_source/csv_file.rb', line 28
def fields
@fields
end
|
Class Method Details
.default_csv_options ⇒ Object
8
9
10
11
12
13
14
15
16
17
|
# File 'lib/remi/data_source/csv_file.rb', line 8
def self.default_csv_options
CSV::DEFAULT_OPTIONS.merge({
headers: true,
header_converters: Remi::FieldSymbolizers[:standard],
converters: [],
col_sep: ',',
encoding: 'UTF-8',
quote_char: '"'
})
end
|
Instance Method Details
#df ⇒ Object
96
97
98
|
# File 'lib/remi/data_source/csv_file.rb', line 96
def df
@dataframe ||= to_dataframe
end
|
36
37
38
|
# File 'lib/remi/data_source/csv_file.rb', line 36
def
@extracted = Array(@extractor.)
end
|
40
41
42
|
# File 'lib/remi/data_source/csv_file.rb', line 40
def
@extracted ||
end
|
#field_symbolizer ⇒ Object
32
33
34
|
# File 'lib/remi/data_source/csv_file.rb', line 32
def field_symbolizer
self.class.default_csv_options[:header_converters]
end
|
#first_line ⇒ Object
63
64
65
66
67
68
|
# File 'lib/remi/data_source/csv_file.rb', line 63
def first_line
@first_line ||= File.open(source_filename) do |f|
f.readline.gsub(/\r/,'')
end
end
|
70
71
72
|
# File 'lib/remi/data_source/csv_file.rb', line 70
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
58
59
60
61
|
# File 'lib/remi/data_source/csv_file.rb', line 58
def source_filename
raise "Multiple source files detected" if .size > 1
@source_filename ||= .first
end
|
48
49
50
|
# File 'lib/remi/cucumber/data_source.rb', line 48
def
@fields.keys.join(@csv_options[:col_sep])
end
|
#stub_row_csv ⇒ Object
52
53
54
|
# File 'lib/remi/cucumber/data_source.rb', line 52
def stub_row_csv
stub_row_array.join(@csv_options[:col_sep])
end
|
#stub_tmp_file ⇒ Object
35
36
37
|
# File 'lib/remi/cucumber/data_source.rb', line 35
def stub_tmp_file
@stub_tmp_file ||= Tempfile.new('stub_tmp_file.csv').path
end
|
#to_dataframe ⇒ Object
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/remi/data_source/csv_file.rb', line 78
def to_dataframe
result_df = nil
.each_with_index do |filename, idx|
@logger.info "Converting #{filename} to a dataframe"
csv_df = Daru::DataFrame.from_csv filename, @csv_options
csv_df[@filename_field] = Daru::Vector.new([filename] * csv_df.size, index: csv_df.index) if @filename_field
if idx == 0
result_df = csv_df
else
result_df = result_df.concat csv_df
end
end
result_df
end
|
74
75
76
|
# File 'lib/remi/data_source/csv_file.rb', line 74
def
(fields.keys - ).empty?
end
|
#write_stub_tmp_file ⇒ Object
39
40
41
42
43
44
45
46
|
# File 'lib/remi/cucumber/data_source.rb', line 39
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
|