Class: Roo::SmarterCSV

Inherits:
Base
  • Object
show all
Defined in:
lib/roo/smarter_csv/smarter_csv_adapter.rb,
lib/roo/smarter_csv/version.rb

Overview

Roo CSV adapter backed by SmarterCSV while preserving Roo’s sheet-style API.

Constant Summary collapse

VERSION =
"1.0.0"
COMPATIBLE_CSV_KEYS =
%i[col_sep row_sep quote_char encoding].freeze
DEFAULT_SMARTER_CSV_OPTIONS =
{
  remove_empty_hashes: false
  # collect_raw_lines: false
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#filenameObject (readonly) Also known as: filename_or_stream

Returns the value of attribute filename.



10
11
12
# File 'lib/roo/smarter_csv/smarter_csv_adapter.rb', line 10

def filename
  @filename
end

#readerObject (readonly)

Returns the value of attribute reader.



10
11
12
# File 'lib/roo/smarter_csv/smarter_csv_adapter.rb', line 10

def reader
  @reader
end

Instance Method Details

#cell(row, col, sheet = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/roo/smarter_csv/smarter_csv_adapter.rb', line 22

def cell(row, col, sheet = nil)
  sheet ||= default_sheet
  read_cells(sheet)
  row, col = normalize(row, col)

  return @header_row[col - 1] if header_row?(row)

  row_hash = sparse_row_for(row)
  return nil unless row_hash

  key = header_key_for(col)
  return nil unless key
  return row_hash[key] if row_hash.key?(key)

  missing_cell_value
end

#celltype(row, col, sheet = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/roo/smarter_csv/smarter_csv_adapter.rb', line 39

def celltype(row, col, sheet = nil)
  sheet ||= default_sheet
  read_cells(sheet)
  row, col = normalize(row, col)

  if header_row?(row)
    value = @header_row[col - 1]
    return value.nil? ? nil : infer_type(value)
  end

  row_hash = sparse_row_for(row)
  return nil unless row_hash

  key = header_key_for(col)
  return nil unless key
  return infer_type(row_hash[key]) if row_hash.key?(key)

  :empty
end

#csv_optionsObject



72
73
74
# File 'lib/roo/smarter_csv/smarter_csv_adapter.rb', line 72

def csv_options
  @options[:csv_options] || {}
end

#row(row_number, sheet = default_sheet) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/roo/smarter_csv/smarter_csv_adapter.rb', line 59

def row(row_number, sheet = default_sheet)
  read_cells(sheet)

  if header_row?(row_number)
    return @header_row.length < last_column(sheet) ? @header_row + Array.new(last_column(sheet) - @header_row.length) : @header_row.dup
  end

  row_hash = sparse_row_for(row_number)
  return [] unless row_hash

  @header_keys.map { |key| row_hash.fetch(key, missing_cell_value) }
end

#set_type(_row, _col, _type, _sheet) ⇒ Object



110
111
112
# File 'lib/roo/smarter_csv/smarter_csv_adapter.rb', line 110

def set_type(_row, _col, _type, _sheet)
  nil
end

#set_value(row, col, value, _sheet) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/roo/smarter_csv/smarter_csv_adapter.rb', line 93

def set_value(row, col, value, _sheet)
  read_cells(default_sheet) unless @cells_read[default_sheet]
  row, col = normalize(row, col)

  if header_row?(row)
    ensure_header_width(col)
    @header_row[col - 1] = value
  else
    ensure_data_row(row)
    ensure_header_width(col)
    @rows[data_row_index(row)][header_key_for(col)] = value
  end

  recalculate_bounds
  value
end

#sheetsObject



18
19
20
# File 'lib/roo/smarter_csv/smarter_csv_adapter.rb', line 18

def sheets
  ["default"]
end

#smarter_csv_optionsObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/roo/smarter_csv/smarter_csv_adapter.rb', line 76

def smarter_csv_options
  @smarter_csv_options ||= begin
    compat = csv_options.each_with_object({}) do |(key, value), result|
      symbol_key = key.to_sym
      result[symbol_key] = value if COMPATIBLE_CSV_KEYS.include?(symbol_key)
    end

    smarter = normalize_option_hash(@options[:smarter_csv])

    (compat.keys & smarter.keys).each do |key|
      warn "roo-smarter_csv: conflicting option #{key} found in csv_options and smarter_csv; using smarter_csv[:#{key}]"
    end

    DEFAULT_SMARTER_CSV_OPTIONS.merge(compat).merge(smarter)
  end
end