Class: Mayak::Csv::Decoder::HashDecoderStrict
- Inherits:
-
T::Struct
- Object
- T::Struct
- Mayak::Csv::Decoder::HashDecoderStrict
- Extended by:
- T::Generic, T::Sig
- Includes:
- Mayak::Csv::Decoder
- Defined in:
- lib/mayak/csv/decoder.rb
Constant Summary collapse
- Value =
type_member {{ fixed: T::Hash[String, String] }}
Instance Method Summary collapse
Methods included from Mayak::Csv::Decoder
hash_decoder, hash_decoder_strict, #map, #map_try
Instance Method Details
#decode(csv) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/mayak/csv/decoder.rb', line 101 def decode(csv) lines = begin case csv when String then csv.split("\n") else csv.to_a end end header, *rows = lines return Mayak::Monads::Try::Success.new([]) if rows.empty? return Mayak::Monads::Try::Success.new([]) if header.nil? || header.empty? keys = header.split(separator) parse_results = rows.map.with_index do |row, index| values = row.split(separator) if values.length != keys.length Mayak::Monads::Try::Failure.new( ::Mayak::Csv::ParseError.new((keys.length, row.length, index)) ) else keys.zip(values).to_h end end Mayak::Monads::Try.sequence(parse_results).map(&:to_h) end |