Module: Nebulous::Input::Parsing

Included in:
Parser
Defined in:
lib/nebulous/input/parsing.rb

Instance Method Summary collapse

Instance Method Details

#chunkObject



17
18
19
# File 'lib/nebulous/input/parsing.rb', line 17

def chunk
  @chunk ||= Chunk.new chunk_options
end

#chunk_optionsObject



53
54
55
56
57
# File 'lib/nebulous/input/parsing.rb', line 53

def chunk_options
  Hash.new.tap do |attrs|
    attrs[:size] = options.chunk.to_i if options.chunk
  end
end

#iterate(&block) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/nebulous/input/parsing.rb', line 36

def iterate(&block)
  while !file.eof?
    break if limit?
    chunk << replace_keys(parse_row)
    yield_chunk(chunk, &block) if block_given? && options.chunk
  end

  @chunk.to_a
end

#limit?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/nebulous/input/parsing.rb', line 25

def limit?
  options.limit && options.limit == @index
end

#parse_rowObject



4
5
6
7
# File 'lib/nebulous/input/parsing.rb', line 4

def parse_row
  sequence
  Row.parse(read_complete_line, options).to_numeric.merge(@headers)
end

#raw_headersObject



9
10
11
# File 'lib/nebulous/input/parsing.rb', line 9

def raw_headers
  Row.parse(readline, options)
end

#read_headersObject



13
14
15
# File 'lib/nebulous/input/parsing.rb', line 13

def read_headers
  @headers ||= Row.headers(readline, options) if options[:headers]
end

#replace_keys(row) ⇒ Object



46
47
48
49
50
51
# File 'lib/nebulous/input/parsing.rb', line 46

def replace_keys(row)
  return row unless options.mapping
  row.map do |key, value|
    [options.mapping[key], value] if options.mapping.has_key?(key)
  end.compact.to_h
end

#sequenceObject



21
22
23
# File 'lib/nebulous/input/parsing.rb', line 21

def sequence
  @index += 1
end

#yield_chunk(chunk, &_block) ⇒ Object



29
30
31
32
33
34
# File 'lib/nebulous/input/parsing.rb', line 29

def yield_chunk(chunk, &_block)
  if chunk.full? || file.eof?
    yield chunk
    @chunk = nil
  end
end