Module: YouPlot::DSV
- Defined in:
- lib/youplot/dsv.rb
Overview
Module to handle DSV (Delimiter-separated values) format. Extract header and series.
Class Method Summary collapse
- .get_headers(arr, headers, transpose) ⇒ Object
- .get_series(arr, headers, transpose) ⇒ Object
- .parse(input, delimiter, headers, transpose) ⇒ Object
-
.transpose2(arr) ⇒ Object
Transpose different sized ruby arrays stackoverflow.com/q/26016632.
Class Method Details
.get_headers(arr, headers, transpose) ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/youplot/dsv.rb', line 57 def get_headers(arr, headers, transpose) # header(-) return nil unless headers # header(+) trenspose(+) return arr.map(&:first) if transpose # header(+) transpose(-) arr[0] end |
.get_series(arr, headers, transpose) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/youplot/dsv.rb', line 68 def get_series(arr, headers, transpose) # header(-) unless headers return arr if transpose return transpose2(arr) end # header(+) but no element in the series. # TODO: should raise error? return Array.new(arr[0].size, []) if arr.size == 1 # header(+) transpose(+) return arr.map { |row| row[1..-1] } if transpose # header(+) transpose(-) transpose2(arr[1..-1]) end |
.parse(input, delimiter, headers, transpose) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/youplot/dsv.rb', line 11 def parse(input, delimiter, headers, transpose) # Parse as CSV arr = CSV.parse(input, col_sep: delimiter) # Remove blank lines arr.delete_if do |i| i == [] or i.all?(&:nil?) end # get header headers = get_headers(arr, headers, transpose) # get series series = get_series(arr, headers, transpose) # Return if No header return Data.new(headers, series) if headers.nil? # Warn if header contains nil warn "\e[35mHeaders contains nil in it.\e[0m" if headers.include?(nil) # Warn if header contains '' warn "\e[35mHeaders contains \"\" in it.\e[0m" if headers.include? '' # Make sure the number of elements in the header matches the number of series. h_size = headers.size s_size = series.size if h_size > s_size warn "\e[35mThe number of headers is greater than the number of series.\e[0m" exit 1 if YouPlot.run_as_executable? elsif h_size < s_size warn "\e[35mThe number of headers is less than the number of series.\e[0m" exit 1 if YouPlot.run_as_executable? end Data.new(headers, series) if h_size == s_size end |
.transpose2(arr) ⇒ Object
Transpose different sized ruby arrays stackoverflow.com/q/26016632
53 54 55 |
# File 'lib/youplot/dsv.rb', line 53 def transpose2(arr) Array.new(arr.map(&:length).max) { |i| arr.map { |e| e[i] } } end |