Class: DataImp::Parser

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

Direct Known Subclasses

Csv, Spreadsheet, Stream

Defined Under Namespace

Classes: Csv, Json, Ods, Spreadsheet, Stream, Xls, Xlsx, Yaml

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename = nil) ⇒ Parser

Returns a new instance of Parser.



5
6
7
# File 'lib/data_imp/parser.rb', line 5

def initialize filename=nil
  @filename = filename
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



3
4
5
# File 'lib/data_imp/parser.rb', line 3

def filename
  @filename
end

Class Method Details

.find_parser(type) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/data_imp/parser.rb', line 9

def self.find_parser type
  return self if type.blank?
  begin
    const_get type.camelize
  rescue NameError => e
    if require_relative "parser/#{type.underscore}"
      retry 
    end
  end
rescue LoadError => e
  raise DataImp::NoParser.new(type)
end

Instance Method Details

#parse(chunk) ⇒ Object



22
23
24
# File 'lib/data_imp/parser.rb', line 22

def parse chunk
  chunk # assume chunk is a hash
end

#process(input, &block) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/data_imp/parser.rb', line 26

def process input, &block
  index = 1
  input.each do |chunk|
    hash = parse(chunk)
    yield hash, index
    index += 1
  end
end

#process_file(&block) ⇒ Object



35
36
37
38
39
# File 'lib/data_imp/parser.rb', line 35

def process_file &block
  open(filename) do |file|
    process file, &block
  end
end