class Parxer::BaseParser
Attributes
attribute[R]
file[R]
prev_row[R]
row[R]
value[R]
Public Instance Methods
extract_raw_attr_value(value)
click to toggle source
# File lib/parxer/parsers/base_parser.rb, line 33 def extract_raw_attr_value(value) value end
file_extension()
click to toggle source
# File lib/parxer/parsers/base_parser.rb, line 45 def file_extension ext = File.extname(file.to_s).delete(".") return if ext.blank? ext.to_sym end
header()
click to toggle source
# File lib/parxer/parsers/base_parser.rb, line 37 def header @header ||= raw_rows.first end
raw_rows()
click to toggle source
# File lib/parxer/parsers/base_parser.rb, line 29 def raw_rows raise Parxer::ParserError.new("not implemented") end
rows_count()
click to toggle source
# File lib/parxer/parsers/base_parser.rb, line 41 def rows_count raw_rows.count end
run(file)
click to toggle source
# File lib/parxer/parsers/base_parser.rb, line 15 def run(file) @file = file return unless validate_file row_class = Parxer::RowBuilder.build(attribute_ids) Enumerator.new do |enum| for_each_raw_row do |raw_row, idx| @row = row_class.new(idx: idx) parse_row(raw_row) enum << row @prev_row = row end end end
Private Instance Methods
for_each_raw_row() { |raw_row_to_hash(raw_row), idx + 1| ... }
click to toggle source
# File lib/parxer/parsers/base_parser.rb, line 64 def for_each_raw_row raw_rows.each_with_index do |raw_row, idx| next if idx.zero? yield(raw_row_to_hash(raw_row), idx + 1) end end
parse_row(raw_row)
click to toggle source
# File lib/parxer/parsers/base_parser.rb, line 53 def parse_row(raw_row) raw_row.each do |attribute_name, value| @value = row.send("#{attribute_name}=", value) @attribute = find_attribute(attribute_name) format_attribute_value if validate_row_attribute end validate_row after_parse_row end
raw_row_to_hash(raw_row)
click to toggle source
# File lib/parxer/parsers/base_parser.rb, line 71 def raw_row_to_hash(raw_row) pos = 0 attributes.inject({}) do |memo, column| memo[column.id.to_sym] = extract_raw_attr_value(raw_row[pos]) pos += 1 memo end end