Class: CSVConverter::Converters::HashConverter
- Inherits:
-
BaseConverter
- Object
- BaseConverter
- CSVConverter::Converters::HashConverter
- Defined in:
- lib/csv_converter/converters/hash_converter.rb
Overview
Converts a string with key pair values into ruby hashes
Instance Attribute Summary
Attributes inherited from BaseConverter
Instance Method Summary collapse
-
#call ⇒ Hash
Converts data into a hash by splitting the string on the item_separator to get the items and then by spliting the items on key_value_separator to get the key/value.
-
#call! ⇒ Hash
Converts data into a hash by splitting the string on the item_separator to get the items and then by spliting the items on key_value_separator to get the key/value.
-
#initialize(raw_data, options = {}) ⇒ HashConverter
constructor
A new instance of HashConverter.
Methods inherited from BaseConverter
Constructor Details
#initialize(raw_data, options = {}) ⇒ HashConverter
A new instance of HashConverter.
13 14 15 16 17 |
# File 'lib/csv_converter/converters/hash_converter.rb', line 13 def initialize(raw_data, = {}) super(raw_data, ) end |
Instance Method Details
#call ⇒ Hash
Converts data into a hash by splitting the string on the item_separator to get the items and then by spliting the items on key_value_separator to get the key/value.
22 23 24 25 26 |
# File 'lib/csv_converter/converters/hash_converter.rb', line 22 def call call! rescue CSVConverter::Error nullable_object end |
#call! ⇒ Hash
Converts data into a hash by splitting the string on the item_separator to get the items and then by spliting the items on key_value_separator to get the key/value.
31 32 33 34 35 36 37 |
# File 'lib/csv_converter/converters/hash_converter.rb', line 31 def call! data.split([:item_separator]).map do |items| items.split([:key_value_separator]).map(&:strip) end.to_h rescue StandardError => e raise CSVConverter::Error.new(e., ) end |