class Schema::CSVParser

Schema::CSVParser is used to create schema models from a csv

Public Class Methods

new(csv, schema_class, headers = nil) click to toggle source
# File lib/schema/csv_parser.rb, line 8
def initialize(csv, schema_class, headers = nil)
  @csv = csv
  @schema_class = schema_class
  @headers = (headers || csv.shift) || []
  @headers = @headers.map(&:strip)
  @mapped_headers = schema_class.map_headers_to_attributes(@headers)
end

Public Instance Methods

each() { |schema| ... } click to toggle source
# File lib/schema/csv_parser.rb, line 27
def each
  while (schema = shift)
    yield schema
  end
end
get_mapped_headers(mapped_headers) click to toggle source
# File lib/schema/csv_parser.rb, line 33
def get_mapped_headers(mapped_headers)
  indexed_headers = []
  mapped_headers.each do |_, info|
    if (index = info[:index])
      indexed_headers << @headers[index]
    elsif (indexes = info[:indexes])
      indexed_headers += indexes.map { |i| @headers[i] }
    else
      indexed_headers += get_mapped_headers(info)
    end
  end
  indexed_headers
end
missing_fields(required_fields) click to toggle source
# File lib/schema/csv_parser.rb, line 16
def missing_fields(required_fields)
  required_fields - get_mapped_headers(@mapped_headers)
end
Also aliased as: missing_headers
missing_headers(required_fields)
Alias for: missing_fields
shift() click to toggle source
# File lib/schema/csv_parser.rb, line 21
def shift
  return unless (row = @csv.shift)

  @schema_class.from_array(row, @mapped_headers)
end