Class: Nebulous::DelimiterDetector
- Inherits:
-
Object
- Object
- Nebulous::DelimiterDetector
- 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
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #detect ⇒ Object
- #detect_column_delimiter ⇒ Object
- #detect_line_delimiter ⇒ Object
-
#initialize(path, *args) ⇒ DelimiterDetector
constructor
A new instance of DelimiterDetector.
Constructor Details
#initialize(path, *args) ⇒ DelimiterDetector
Returns a new instance of DelimiterDetector.
13 14 15 16 17 18 |
# File 'lib/nebulous/delimiter_detector.rb', line 13 def initialize(path, *args) @path = path @options = args. raise ArgumentError unless File.exists?(@path) end |
Instance Attribute Details
#path ⇒ Object (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
#detect ⇒ Object
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_delimiter ⇒ Object
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_delimiter ⇒ Object
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 |