class Reading::Csv::Parse::ParseLine
ParseLine
is a base class that holds common behaviors.
Public Class Methods
new(merged_config)
click to toggle source
# File lib/reading/csv/parse_line.rb, line 13 def initialize(merged_config) @line = nil @config ||= merged_config setup_default after_initialize end
Public Instance Methods
call(line, &postprocess)
click to toggle source
# File lib/reading/csv/parse_line.rb, line 32 def call(line, &postprocess) @line = line before_parse titles = [] items = split_by_format_emojis.map do |name| data = item_data(name) if titles.include?(name) raise InvalidItemError, "A title must not appear more than once in the list" end titles << data[:title] if block_given? postprocess.call(data) else data end end.compact items rescue InvalidItemError, StandardError => e # TODO instead of rescuing StandardError here, test missing # initial/middle columns in ParseRegularLine#set_columns, and raise # appropriate errors if possible. unless e.is_a? InvalidItemError if config.fetch(:errors).fetch(:catch_all_errors) e = InvalidItemError.new("A line could not be parsed. Check this line") else raise e end end e.handle(source: line, config: config) [] ensure # reset to pre-call state. initialize(config) end
setup_default()
click to toggle source
# File lib/reading/csv/parse_line.rb, line 20 def setup_default @default = config.fetch(:item).fetch(:template) .map do |attribute, value| if value.is_a?(Array) && value.first.is_a?(Hash) [attribute, []] else [attribute, value] end end.to_h end
Private Instance Methods
after_initialize()
click to toggle source
# File lib/reading/csv/parse_line.rb, line 82 def after_initialize end
before_parse()
click to toggle source
# File lib/reading/csv/parse_line.rb, line 85 def before_parse end
item_data(name)
click to toggle source
# File lib/reading/csv/parse_line.rb, line 92 def item_data(name) raise NotImplementedError, "#{self.class} should have implemented #{__method__}" end
multi_items_to_be_split_by_format_emojis()
click to toggle source
# File lib/reading/csv/parse_line.rb, line 88 def multi_items_to_be_split_by_format_emojis raise NotImplementedError, "#{self.class} should have implemented #{__method__}" end
split_by_format_emojis()
click to toggle source
# File lib/reading/csv/parse_line.rb, line 69 def split_by_format_emojis multi_items_to_be_split_by_format_emojis .split(config.fetch(:csv).fetch(:regex).fetch(:formats_split)) .tap do |names| names.first.sub!(config.fetch(:csv).fetch(:regex).fetch(:dnf), "") names.first.sub!(config.fetch(:csv).fetch(:regex).fetch(:progress), "") end .map { |name| name.strip.sub(/\s*[,;]\z/, "") } .partition { |name| name.match?(/\A#{config.fetch(:csv).fetch(:regex).fetch(:formats)}/) } .reject(&:empty?) .first end