Class: Parxer::BaseParser

Inherits:
Object
  • Object
show all
Includes:
Dsl, InheritedResource, ParserAttributes, ParserCallback, ParserConfig, ParserFormatter, ParserValidator
Defined in:
lib/parxer/parsers/base_parser.rb

Direct Known Subclasses

CsvParser, XlsParser

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from InheritedResource

#for_each_ancestor_with_method, #inherited_collection, #inherited_hash, #object_ancestors

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



11
12
13
# File 'lib/parxer/parsers/base_parser.rb', line 11

def attribute
  @attribute
end

#fileObject (readonly)

Returns the value of attribute file.



11
12
13
# File 'lib/parxer/parsers/base_parser.rb', line 11

def file
  @file
end

#prev_rowObject (readonly)

Returns the value of attribute prev_row.



11
12
13
# File 'lib/parxer/parsers/base_parser.rb', line 11

def prev_row
  @prev_row
end

#rowObject (readonly)

Returns the value of attribute row.



11
12
13
# File 'lib/parxer/parsers/base_parser.rb', line 11

def row
  @row
end

#valueObject (readonly)

Returns the value of attribute value.



11
12
13
# File 'lib/parxer/parsers/base_parser.rb', line 11

def value
  @value
end

Instance Method Details

#extract_raw_attr_value(value) ⇒ Object



33
34
35
# File 'lib/parxer/parsers/base_parser.rb', line 33

def extract_raw_attr_value(value)
  value
end

#file_extensionObject



45
46
47
48
49
# 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

#headerObject



37
38
39
# File 'lib/parxer/parsers/base_parser.rb', line 37

def header
  @header ||= raw_rows.first
end

#raw_rowsObject



29
30
31
# File 'lib/parxer/parsers/base_parser.rb', line 29

def raw_rows
  raise Parxer::ParserError.new("not implemented")
end

#rows_countObject



41
42
43
# File 'lib/parxer/parsers/base_parser.rb', line 41

def rows_count
  raw_rows.count
end

#run(file) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# 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