Class: CsvConfig
- Inherits:
-
JsonConfig
- Object
- JsonConfig
- CsvConfig
- Defined in:
- lib/resources/csv.rb
Overview
Parses a csv document Usage: describe csv(‘example.csv’) do
its('name') { should eq(['John', 'Alice']) }
end
This implementation was inspired by a blog post
Instance Attribute Summary
Attributes inherited from JsonConfig
Instance Method Summary collapse
-
#parse(content) ⇒ Object
override file load and parse hash from csv.
- #to_s ⇒ Object
Methods inherited from JsonConfig
#extract_value, #initialize, #method_missing
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
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/resources/csv.rb', line 17 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_s ⇒ Object
29 30 31 |
# File 'lib/resources/csv.rb', line 29 def to_s "Csv #{@path}" end |