Class: Reading::Parsing::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/reading/parsing/parser.rb

Overview

Parses a string containing a row of a CSV reading log, into a hash mirroring the structure of the row. This hash is an intermediate form and not the final item data. It’s the raw material for Parsing::Transformer to generate the final item data.

Below is an example intermediate hash parsed from this row, which has a Rating column, then a Head column containing an author, title, series, and extra info:

3|📕Thomas More - Utopia – trans. Robert Adams – ed. George Logan – in Cambridge History of Political Thought

{

rating: { number: "1" },
head: [{
  author: "Thomas More",
  title: "Utopia",
  series_names: ["Cambridge History of Political Thought"],
  series_volumes: [nil],
  extra_info: ["trans. Robert Adams", "ed. George Logan"],
  format: :print,
}]

}

The hash’s top-level keys are column names. The nested keys come from regex capture group names in each column (for this example, see ::regexes in rating.rb and head.rb in parsing/rows/regular_columns).

All the rest is just details of how the parts of a column are joined:

  • The :head value is an array because Head.split_by_format? is true (because a Head column can potentially contain multiple items). That’s also where { format: :print } comes from.

  • The :series_names and :series_volumes values are arrays because these keys are in Head.flatten_into_arrays, which causes the column’s segments (separated by “ – ”) to be merged into one hash.

Instance Method Summary collapse

Instance Method Details

#parse_row_to_intermediate_hash(string) ⇒ Hash

Parses a row string into a hash that mirrors the structure of the row.

Parameters:

  • string (String)

    a string containing a row of a CSV reading log.

Returns:



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/reading/parsing/parser.rb', line 52

def parse_row_to_intermediate_hash(string)
  columns = extract_columns(string)

  if Config.hash.fetch(:skip_compact_planned) && columns.has_key?(Rows::CompactPlanned::Head)
    return {}
  end

  columns.map { |column, column_string|
    parse_column(column, column_string)
  }.to_h
end