Class: Nebulous::DelimiterDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/nebulous/delimiter_detector.rb

Constant Summary collapse

LINE_DELIMITERS =
[
  [/CRLF/, "\n"],
  [/CR, LF/, "\r"],
  [/CR(?!,)/, "\r"]
]
COLUMN_DELIMITERS =
[',', ';', "\t", '|']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, *args) ⇒ DelimiterDetector

Returns a new instance of DelimiterDetector.

Raises:

  • (ArgumentError)


13
14
15
16
17
18
# File 'lib/nebulous/delimiter_detector.rb', line 13

def initialize(path, *args)
  @path = path
  @options = args.extract_options!

  raise ArgumentError unless File.exists?(@path)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



11
12
13
# File 'lib/nebulous/delimiter_detector.rb', line 11

def path
  @path
end

Instance Method Details

#detectObject



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

def detect
  { col_sep: detect_column_delimiter,
    row_sep: detect_line_delimiter }
end

#detect_column_delimiterObject



25
26
27
28
29
30
31
32
33
34
# File 'lib/nebulous/delimiter_detector.rb', line 25

def detect_column_delimiter
  ln = readline

  column_delimiters.each_with_index do |exp, index|
    counts[index] = ln.split(exp).length - 1
  end

  count = counts.each_with_index.max[1]
  column_delimiters[count]
end

#detect_line_delimiterObject



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

def detect_line_delimiter
  res = Cocaine::CommandLine.new('file', ':path').run(path: path).chomp

  map = line_delimiters.map do |sep|
    sep[1] if res =~ sep[0]
  end.compact

  map.first || line_delimiters[0][1]
end