Class: DataImp::Parser

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

Direct Known Subclasses

Csv, Stream

Defined Under Namespace

Classes: Csv, Json, Stream, 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



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

def find_parser type
  return self if type.blank?
  begin
    type = type.to_s.camelize
    const_get type
  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



25
26
27
# File 'lib/data_imp/parser.rb', line 25

def parse chunk
  chunk # assume chunk is a hash
end

#process(input, &block) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/data_imp/parser.rb', line 29

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

#process_file(&block) ⇒ Object



38
39
40
41
42
# File 'lib/data_imp/parser.rb', line 38

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