class Eatr::Csv::Document
Attributes
schema[R]
Public Class Methods
new(schema_path)
click to toggle source
# File lib/eatr/csv/document.rb, line 16 def initialize(schema_path) @schema = Schema.new(YAML.load(File.read(schema_path))) end
Public Instance Methods
parse(csv_document_path)
click to toggle source
# File lib/eatr/csv/document.rb, line 20 def parse(csv_document_path) objects = [] CSV.foreach(csv_document_path, headers: true) do |row| obj = @schema.to_struct.new @schema.fields.each do |field| obj.public_send("#{field.name}=", value_at(row, field)) end objects << obj end objects end
Private Instance Methods
value_at(row, field)
click to toggle source
# File lib/eatr/csv/document.rb, line 38 def value_at(row, field) if text = row[field.csv_header] parse_value(field, text) elsif field.value parse_value(field, field.value) elsif field.required? raise ValueNotFound, "Unable to find '#{field.name}' with header '#{field.csv_header}'" end end