Class: StrongCSV

Inherits:
Object
  • Object
show all
Defined in:
lib/strong_csv.rb,
lib/strong_csv/let.rb,
lib/strong_csv/row.rb,
lib/strong_csv/version.rb,
lib/strong_csv/types/base.rb,
lib/strong_csv/types/time.rb,
lib/strong_csv/types/float.rb,
lib/strong_csv/types/union.rb,
lib/strong_csv/type_wrapper.rb,
lib/strong_csv/types/string.rb,
lib/strong_csv/value_result.rb,
lib/strong_csv/types/boolean.rb,
lib/strong_csv/types/integer.rb,
lib/strong_csv/types/literal.rb,
lib/strong_csv/types/optional.rb

Overview

rubocop:disable Style/Documentation

Defined Under Namespace

Modules: Types Classes: Error, Let, Row, TypeWrapper, ValueResult

Constant Summary collapse

VERSION =
"0.8.4"

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ StrongCSV

Returns a new instance of StrongCSV.



42
43
44
45
# File 'lib/strong_csv.rb', line 42

def initialize(&block)
  @let = Let.new
  @let.instance_eval(&block) if block_given?
end

Instance Method Details

#parse(csv, **options) ⇒ Object

Parameters:

  • csv (String, IO)
  • options (Hash)

    CSV options for parsing.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/strong_csv.rb', line 49

def parse(csv, **options)
  # NOTE: Some options are overridden here to ensure that StrongCSV can handle parsed values correctly.
  options.delete(:nil_value)
  options = options.merge(headers: @let.headers, header_converters: :symbol)
  csv = CSV.new(csv, **options)

  @let.pickers.each_value do |picker|
    picker.call(csv)
    csv.rewind
  end

  if block_given?
    csv.each do |row|
      yield Row.new(row: row, types: @let.types, lineno: csv.lineno)
    end
  else
    csv.map { |row| Row.new(row: row, types: @let.types, lineno: csv.lineno) }
  end
end