Class: Eatr::Csv::Document

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
ParseValue
Defined in:
lib/eatr/csv/document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ParseValue

#parse_value

Constructor Details

#initialize(schema_path) ⇒ Document

Returns a new instance of Document.



16
17
18
# File 'lib/eatr/csv/document.rb', line 16

def initialize(schema_path)
  @schema = Schema.new(YAML.load(File.read(schema_path)))
end

Instance Attribute Details

#schemaObject (readonly)

Returns the value of attribute schema.



11
12
13
# File 'lib/eatr/csv/document.rb', line 11

def schema
  @schema
end

Instance Method Details

#parse(csv_document_path) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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