Module: Meibo::DataModel::ClassMethods

Defined in:
lib/meibo/data_model.rb

Instance Method Summary collapse

Instance Method Details

#parse(csv) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/meibo/data_model.rb', line 8

def parse(csv)
  return to_enum(:parse, csv) unless block_given?

  validate_header_fields(CSV.parse_line(csv))

  CSV.parse(csv, encoding: Meibo::CSV_ENCODING, headers: true, converters: parser_converters,
                 header_converters: header_converters).each do |row|
    yield new(**row.to_h)
  end
end

#validate_header_fields(actual_header_fields) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/meibo/data_model.rb', line 19

def validate_header_fields(actual_header_fields)
  missing_header_fields = header_fields - actual_header_fields
  unless missing_header_fields.empty?
    message = "missing header fields: #{missing_header_fields.join(",")}"
    raise MissingHeaderFieldsError.new(message, missing_header_fields: missing_header_fields)
  end
  raise ScrambledHeaderFieldsError unless actual_header_fields.take(header_fields.size) == header_fields
end