Class: CsvConfig

Inherits:
JsonConfig show all
Defined in:
lib/resources/csv.rb

Overview

Parses a csv document This implementation was inspired by a blog post

Instance Attribute Summary

Attributes inherited from JsonConfig

#params

Instance Method Summary collapse

Methods inherited from JsonConfig

#initialize, #method_missing, #value

Constructor Details

This class inherits a constructor from JsonConfig

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class JsonConfig

Instance Method Details

#parse(content) ⇒ Object

override file load and parse hash from csv



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/resources/csv.rb', line 18

def parse(content)
  require 'csv'
  # convert empty field to nil
  CSV::Converters[:blank_to_nil] = lambda do |field|
    field && field.empty? ? nil : field
  end
  # implicit conversion of values
  csv = CSV.new(content, headers: true, converters: [:all, :blank_to_nil])
  # convert to hash
  csv.to_a.map(&:to_hash)
end

#to_sObject



30
31
32
# File 'lib/resources/csv.rb', line 30

def to_s
  "Csv #{@path}"
end