Class: Valigator::CSV::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/valigator/csv/validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Validator

Returns a new instance of Validator.



8
9
10
11
12
13
# File 'lib/valigator/csv/validator.rb', line 8

def initialize(filename)
  @filename = filename
  @current_row_number = 0
  @errors = []
  @config = default_config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



4
5
6
# File 'lib/valigator/csv/validator.rb', line 4

def config
  @config
end

#errorsObject (readonly)

Returns the value of attribute errors.



4
5
6
# File 'lib/valigator/csv/validator.rb', line 4

def errors
  @errors
end

#filenameObject (readonly)

Returns the value of attribute filename.



4
5
6
# File 'lib/valigator/csv/validator.rb', line 4

def filename
  @filename
end

Instance Method Details

#validate(options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/valigator/csv/validator.rb', line 17

def validate(options = {})
  config.merge! options
  @current_row_number = csv_options(options)[:headers] ? 1 : 0

  ::CSV.foreach(filename, **csv_options(options)) do |row|
    @current_row_number += 1
    validate_fields row, options
    validate_row row, options

    stop_if_error_limit_reached
  end
rescue ErrorsLimitReachedError
rescue ::CSV::MalformedCSVError, Encoding::CompatibilityError, ArgumentError => error
  raise if unrelated_error?(error)

  errors << CSV::Error.new(error)
end