Class: DS::Converter::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/ds/converter/writer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#count ⇒ Object (readonly)

Returns the value of attribute count.



7
8
9
# File 'lib/ds/converter/writer.rb', line 7

def count
  @count
end

#output ⇒ Object (readonly)

Returns the value of attribute output.



8
9
10
# File 'lib/ds/converter/writer.rb', line 8

def output
  @output
end

#validator ⇒ Object (readonly)

Returns the value of attribute validator.



9
10
11
# File 'lib/ds/converter/writer.rb', line 9

def validator
  @validator
end

Instance Method Details

#add_error(row_num, error) ⇒ Object



41
42
43
# File 'lib/ds/converter/writer.rb', line 41

def add_error row_num, error
  @errors << [row_num, error]
end

#errors ⇒ Object



45
46
47
# File 'lib/ds/converter/writer.rb', line 45

def errors
  @errors.dup
end

#initializer(output_io, row_validator) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/ds/converter/writer.rb', line 12

def initializer output_io, row_validator
  @output = output_io
  @count = 1
  @validator = row_validator
  @valid = true
  @errors = []
end

#valid? ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/ds/converter/writer.rb', line 31

def valid?
  errors.blank?
end

#validate_row(row_num, row) ⇒ Object



35
36
37
38
39
# File 'lib/ds/converter/writer.rb', line 35

def validate_row row_num, row
  error = validator.row_valid? row
  return unless row
  add_error row_num, error
end

#write ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/ds/converter/writer.rb', line 20

def write
  converter.convert do |row|
    CSV.open output, "w", headers: true do |csv|
      csv << DS::HEADINGS if count == 1
      count += 1
      validate_row count, row
      csv << row
    end
  end
end