Class: ChartHelpers::Parsers::Gantt

Inherits:
Object
  • Object
show all
Defined in:
lib/chart_helpers/parsers/gantt.rb

Constant Summary collapse

GANTT_LINE_REGEX =

This should match “title” :a1, 0.000, 0.001

%r{
  (?<title>.+)
  (?<group>:\w+)
  ,\s+(?<start>[[^,]+]+)
  ,\s+(?<end>[[^,]+]+)
}x

Class Method Summary collapse

Class Method Details

.parse(lines) ⇒ Object



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
50
# File 'lib/chart_helpers/parsers/gantt.rb', line 14

def self.parse(lines)
  title = 'Gantt Chart'
  date_format = nil
  number_format = nil
  data = []

  while line = lines.shift
    next if line.empty? || line.nil?
    line.strip!

    case line
    when /\Atitle/
      if title == 'Gantt Chart'
        # Line will be like:
        # `title THIS IS MY TITLE`
        # We would want "THIS IS MY TITLE"
        title = line.split(' ')[1..-1].join(' ')
      else
        data << parse_line(line)
      end
    when /\AdateFormat/
      # Line will be like:
      # `dateFormat s.SSS`
      # We would want "s.SSS"
      date_format = line.split(' ')[1..-1].join(' ')
    when /\AnumberFormat/
      # Line will be like:
      # `numberFormat s.SSS`
      # We would want "s.SSS"
      number_format = line.split(' ')[1..-1].join(' ')
    else
      data << parse_line(line, date: !date_format.nil?)
    end
  end

  [title, date_format, number_format, data]
end

.parse_line(line, date: false) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/chart_helpers/parsers/gantt.rb', line 52

def self.parse_line(line, date: false)
  match_data = line.match(GANTT_LINE_REGEX)
  start_val, end_val = if date
    [DateTime.parse(match_data[:start]).to_time.to_f, DateTime.parse(match_data[:end]).to_time.to_f]
  else
    [match_data[:start].to_f, match_data[:end].to_f]
  end
  { title: CGI.escapeHTML(match_data[:title]).strip, start: start_val, end: end_val }
end