Class: BulkProcessor::ValidatedCSV
- Inherits:
-
Object
- Object
- BulkProcessor::ValidatedCSV
- Defined in:
- lib/bulk_processor/validated_csv.rb
Overview
A Wrapper on CSV that validates column headers.
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
Instance Method Summary collapse
-
#initialize(stream, required_headers, optional_headers) ⇒ ValidatedCSV
constructor
A new instance of ValidatedCSV.
-
#valid? ⇒ true|false
True iff: * All required columns are present * No column exists that isn’t a required or optional column * No column heading is blank.
Constructor Details
#initialize(stream, required_headers, optional_headers) ⇒ ValidatedCSV
Returns a new instance of ValidatedCSV.
21 22 23 24 25 26 |
# File 'lib/bulk_processor/validated_csv.rb', line 21 def initialize(stream, required_headers, optional_headers) @stream = stream @required_headers = required_headers @optional_headers = optional_headers @errors = [] end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
19 20 21 |
# File 'lib/bulk_processor/validated_csv.rb', line 19 def errors @errors end |
Instance Method Details
#valid? ⇒ true|false
Returns true iff:
-
All required columns are present
-
No column exists that isn’t a required or optional column
-
No column heading is blank.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/bulk_processor/validated_csv.rb', line 32 def valid? return false if csv.nil? @errors = [] if missing_headers.any? errors << "Missing required column(s): #{missing_headers.join(', ')}".freeze end if extra_headers.any? errors << "Unrecognized column(s) found: #{extra_headers.join(', ')}".freeze end if csv.headers.any? { |header| header.nil? || header.strip == '' } errors << MISSING_COLUMN_MESSAGE end errors.empty? end |