Class: StrongCSV::Row

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/strong_csv/row.rb

Overview

Row is a representation of a row in a CSV file, which has casted values with specified types.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(row:, types:, lineno:) ⇒ Row

Returns a new instance of Row.

Parameters:

  • row (Array<String>, CSV::Row)
  • types (Hash)
  • lineno (Integer)

19
20
21
22
23
24
25
26
27
28
# File 'lib/strong_csv/row.rb', line 19

def initialize(row:, types:, lineno:)
  @values = {}
  @errors = {}
  @lineno = lineno
  types.each do |wrapper|
    cell = row[wrapper.name]
    @values[wrapper.name], error = wrapper.cast(cell)
    @errors[wrapper.name] = error if error
  end
end

Instance Attribute Details

#errorsHash (readonly)

Returns:

  • (Hash)

11
12
13
# File 'lib/strong_csv/row.rb', line 11

def errors
  @errors
end

#linenoInteger (readonly)

Returns:

  • (Integer)

14
15
16
# File 'lib/strong_csv/row.rb', line 14

def lineno
  @lineno
end

Instance Method Details

#valid?Boolean

It returns true if the row has no errors.

Returns:

  • (Boolean)

33
34
35
# File 'lib/strong_csv/row.rb', line 33

def valid?
  @errors.empty?
end