Class: Nebulous::Parser

Inherits:
Object
  • Object
show all
Includes:
Input::Delimiters, Input::Parsing, Input::Reader
Defined in:
lib/nebulous/parser.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  col_sep: nil,
  row_sep: nil,
  quote_char: '"',
  comment_exp: /^#/,
  chunk: false,
  headers: true,
  mapping: nil,
  limit: false,
  encoding: Encoding::UTF_8.to_s
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Input::Delimiters

#delimiters

Methods included from Input::Parsing

#chunk, #chunk_options, #iterate, #limit?, #parse_row, #raw_headers, #read_headers, #replace_keys, #sequence, #yield_chunk

Methods included from Input::Reader

#encoding, #line_terminator, #read_complete_line, #read_input, #readline

Constructor Details

#initialize(file, *args) ⇒ Parser

Returns a new instance of Parser.



22
23
24
25
26
27
28
29
# File 'lib/nebulous/parser.rb', line 22

def initialize(file, *args)
  opts = args.extract_options!

  @options = OpenStruct.new DEFAULT_OPTIONS.merge(opts)
  @file = read_input(file)

  merge_delimiters
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



19
20
21
# File 'lib/nebulous/parser.rb', line 19

def file
  @file
end

#optionsObject (readonly)

Returns the value of attribute options.



20
21
22
# File 'lib/nebulous/parser.rb', line 20

def options
  @options
end

Instance Method Details

#headersObject



31
32
33
34
# File 'lib/nebulous/parser.rb', line 31

def headers
  @file.rewind
  raw_headers
end

#process(&block) ⇒ Object



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

def process(&block)
  @index = 0
  read_headers if options[:headers]
  iterate(&block)
ensure
  reset
  file.rewind
end