Class: LcpRuby::Import::FileParser

Inherits:
Object
  • Object
show all
Defined in:
lib/lcp_ruby/import/file_parser.rb

Defined Under Namespace

Classes: ParseError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io_or_path, format:, csv_delimiter: ",", csv_encoding: "auto") ⇒ FileParser

Returns a new instance of FileParser.



19
20
21
22
23
24
25
# File 'lib/lcp_ruby/import/file_parser.rb', line 19

def initialize(io_or_path, format:, csv_delimiter: ",", csv_encoding: "auto")
  @io_or_path = io_or_path
  @format = format.to_s
  @csv_delimiter = csv_delimiter
  @csv_encoding = csv_encoding
  @tempfiles = []
end

Class Method Details

.each_row(io_or_path, format:, sheet_name: nil, csv_delimiter: ",", csv_encoding: "auto", &block) ⇒ Object

Yields { header => value } hashes for each row, streaming.



15
16
17
# File 'lib/lcp_ruby/import/file_parser.rb', line 15

def self.each_row(io_or_path, format:, sheet_name: nil, csv_delimiter: ",", csv_encoding: "auto", &block)
  new(io_or_path, format:, csv_delimiter:, csv_encoding:).each_row(sheet_name:, &block)
end

.parse_metadata(io_or_path, format:, csv_delimiter: ",", csv_encoding: "auto") ⇒ Object

Parse file metadata (headers, row count, sheets) without loading all data. Returns { headers: […], row_count: N, sheets: […] }



10
11
12
# File 'lib/lcp_ruby/import/file_parser.rb', line 10

def self.(io_or_path, format:, csv_delimiter: ",", csv_encoding: "auto")
  new(io_or_path, format:, csv_delimiter:, csv_encoding:).
end

Instance Method Details

#each_row(sheet_name: nil, &block) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/lcp_ruby/import/file_parser.rb', line 37

def each_row(sheet_name: nil, &block)
  case @format
  when "csv"  then each_csv_row(&block)
  when "xlsx" then each_xlsx_row(sheet_name:, &block)
  else raise ParseError, "Unsupported format: #{@format}"
  end
ensure
  cleanup_tempfiles!
end

#parse_metadataObject



27
28
29
30
31
32
33
34
35
# File 'lib/lcp_ruby/import/file_parser.rb', line 27

def 
  case @format
  when "csv"  then 
  when "xlsx" then 
  else raise ParseError, "Unsupported format: #{@format}"
  end
ensure
  cleanup_tempfiles!
end