Class: Nebulous::Parser

Inherits:
Object
  • Object
show all
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,
  remove_empty_values: true,
  encoding: Encoding::UTF_8.to_s
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, *args) ⇒ Parser

Returns a new instance of Parser.



19
20
21
22
23
24
25
26
# File 'lib/nebulous/parser.rb', line 19

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.



16
17
18
# File 'lib/nebulous/parser.rb', line 16

def file
  @file
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#delimitersObject



37
38
39
# File 'lib/nebulous/parser.rb', line 37

def delimiters
  @delimiters ||= DelimiterDetector.new(file.path).detect
end

#process(&block) ⇒ Object



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

def process(&block)
  @index = 0
  read_headers
  iterate(&block)
ensure
  reset
  file.rewind
end