Class: Reading::Parsing::Transformer
- Defined in:
- lib/reading/parsing/transformer.rb
Overview
Transforms an intermediate hash (parsed from a CSV row) into item data. While the intermediate hash mirrors the structure of a row, the output of Transformer is based around item attributes, which are listed in Config#default_config[:template] and in the files in parsing/attributes.
Instance Method Summary collapse
-
#initialize ⇒ Transformer
constructor
A new instance of Transformer.
-
#transform_intermediate_hash_to_item_hashes(parsed_row) ⇒ Array<Hash>
Transforms the intermediate hash of a row into item data.
Constructor Details
#initialize ⇒ Transformer
Returns a new instance of Transformer.
25 26 27 |
# File 'lib/reading/parsing/transformer.rb', line 25 def initialize set_attributes end |
Instance Method Details
#transform_intermediate_hash_to_item_hashes(parsed_row) ⇒ Array<Hash>
Transforms the intermediate hash of a row into item data.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/reading/parsing/transformer.rb', line 34 def transform_intermediate_hash_to_item_hashes(parsed_row) if parsed_row[:head].blank? raise InvalidHeadError, "Blank or missing Head column" end template = Config.hash.deep_fetch(:item, :template) parsed_row[:head].map.with_index { |_head, head_index| template.map { |attribute_name, default_value| attribute = attributes.fetch(attribute_name) transformed_value = attribute.transform_from_parsed(parsed_row, head_index) [attribute_name, transformed_value || default_value] }.to_h .compact_by(template:) } end |