Class: FlatKit::Xsv::Reader

Inherits:
Reader
  • Object
show all
Defined in:
lib/flat_kit/xsv/reader.rb

Instance Attribute Summary collapse

Attributes inherited from Reader

#compare_fields, #source

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Reader

create_reader_from_path, create_readers_from_paths, #format_name

Constructor Details

#initialize(source:, compare_fields: :none, **csv_options) ⇒ Reader

Returns a new instance of Reader.



22
23
24
25
26
27
28
29
# File 'lib/flat_kit/xsv/reader.rb', line 22

def initialize(source:, compare_fields: :none, **csv_options)
  super(source: source, compare_fields: compare_fields)
  @input = ::FlatKit::Input.from(source)
  @count = 0
  @csv_options = Reader.default_csv_options.merge(csv_options)
  @fields = nil
  @csv = CSV.new(input.io, **@csv_options)
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



7
8
9
# File 'lib/flat_kit/xsv/reader.rb', line 7

def count
  @count
end

#fieldsObject (readonly)

Returns the value of attribute fields.



8
9
10
# File 'lib/flat_kit/xsv/reader.rb', line 8

def fields
  @fields
end

#inputObject (readonly)

Returns the value of attribute input.



6
7
8
# File 'lib/flat_kit/xsv/reader.rb', line 6

def input
  @input
end

Class Method Details

.default_csv_optionsObject



14
15
16
17
18
19
20
# File 'lib/flat_kit/xsv/reader.rb', line 14

def self.default_csv_options
  {
    headers: :first_row,
    converters: :numeric,
    return_headers: false
  }
end

.format_nameObject



10
11
12
# File 'lib/flat_kit/xsv/reader.rb', line 10

def self.format_name
  ::FlatKit::Xsv::Format.format_name
end

Instance Method Details

#eachObject



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/flat_kit/xsv/reader.rb', line 31

def each
  @csv.each do |row|
    @fields = row.headers if @fields.nil?
    record = ::FlatKit::Xsv::Record.new(data: row, compare_fields: compare_fields)
    @count += 1
    yield record
  end
  input.close
rescue => e
  ::FlatKit.logger.error "Error reading xsv records from #{input.name}: #{e}"
  raise ::FlatKit::Error, e
end