Class: Cure::Extract::Extractor
Instance Attribute Summary collapse
Instance Method Summary
collapse
#clean_dir, #read_file, #with_file, #with_temp_dir
#config, #create_config, #register_config
Methods included from Log
#log_debug, #log_error, #log_info, #log_warn
Constructor Details
#initialize(opts) ⇒ Extractor
Returns a new instance of Extractor.
19
20
21
|
# File 'lib/cure/extract/extractor.rb', line 19
def initialize(opts)
@opts = opts
end
|
Instance Attribute Details
#opts ⇒ Object
16
17
18
|
# File 'lib/cure/extract/extractor.rb', line 16
def opts
@opts
end
|
Instance Method Details
32
33
34
35
36
|
# File 'lib/cure/extract/extractor.rb', line 32
def (file_contents)
parsed_content = parse_csv(file_contents, header: :none)
log_info("Parsed CSV into #{parsed_content.content.length} sections.")
parsed_content
end
|
25
26
27
28
|
# File 'lib/cure/extract/extractor.rb', line 25
def (csv_file_location)
file_contents = read_file(csv_file_location)
(file_contents)
end
|
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/cure/extract/extractor.rb', line 78
def (rows, named_range)
psx = CsvLookup.array_position_lookup(named_range)
ret_val = []
rows.each_with_index do |row, idx|
ret_val << row[psx[0]..psx[1]] if psx[3] == -1 || (idx >= psx[2] && idx <= psx[3])
end
ret_val
end
|
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/cure/extract/extractor.rb', line 57
def (csv_rows)
candidates = config.template.transformations.candidates
candidate_nrs = config.template..required_named_ranges(candidates.map(&:named_range).uniq)
candidate_nrs.map do |nr|
{
"rows" => (csv_rows, nr["section"]),
"name" => nr["name"]
}
end
end
|
71
72
73
74
75
|
# File 'lib/cure/extract/extractor.rb', line 71
def (csv_rows)
config.template..variables.each_with_object({}) do |variable, hash|
hash[variable["name"]] = lookup_location(csv_rows, variable["location"])
end
end
|
#handle_row(row_idx, row, psx) ⇒ Array?
103
104
105
106
107
|
# File 'lib/cure/extract/extractor.rb', line 103
def handle_row(row_idx, row, psx)
return nil unless psx[3] == -1 || (row_idx >= psx[2] && row_idx <= psx[3])
row[psx[0]..psx[1]]
end
|
#lookup_location(rows, variable_location) ⇒ Object
#parse_csv(file_contents, opts = {}) ⇒ WrappedCSV
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/cure/extract/extractor.rb', line 43
def parse_csv(file_contents, opts={})
csv_rows = []
Rcsv.parse(file_contents, opts) { |row| csv_rows << row }
result = WrappedCSV.new
result.content = (csv_rows)
result.variables = (csv_rows)
result
end
|