Class: Apfel::DotStringsParser

Inherits:
Object
  • Object
show all
Defined in:
lib/apfel/dot_strings_parser.rb

Constant Summary collapse

KEY =
"KEY"
COMMENT =
"COMMENT"

Instance Method Summary collapse

Constructor Details

#initialize(read_file, parsed_file = ParsedDotStrings.new) ⇒ DotStringsParser

Returns a new instance of DotStringsParser.



8
9
10
11
# File 'lib/apfel/dot_strings_parser.rb', line 8

def initialize(read_file, parsed_file = ParsedDotStrings.new)
  @read_file = read_file
  @parsed_file = parsed_file
end

Instance Method Details

#parse_fileObject



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
50
51
52
# File 'lib/apfel/dot_strings_parser.rb', line 13

def parse_file
  state = KEY
  current_comment = nil
  comments_for_keys = {}
  @read_file.each do |content_line|
    current_line = Line.new(content_line)
    next if current_line.empty_line? && current_line.in_comment == false

    #State machine to parse the comments
    case state
    when KEY
      if current_line.whole_comment?
        unless current_line.whole_comment.strip == 'No comment provided by engineer.'
          current_comment = current_line.whole_comment
        end
      elsif current_line.key_value_pair? && current_comment
        comments_for_keys[current_line.key] = current_comment
        current_comment = nil
      elsif current_line.open_comment?
        current_comment = current_line.open_comment + "\n"
        state = COMMENT
      end
    when COMMENT
      if current_line.close_comment?
        current_comment += current_line.close_comment
        state = KEY
      else
        current_line.in_comment = true
        current_comment = current_comment + current_line.content + "\n"
      end
    end

    unless current_line.is_comment?
      @parsed_file.kv_pairs << KVPair.new(current_line, comments_for_keys[current_line.key])
    end
  end

  raise "Invalid .string file: Unterminated comment" unless state == KEY
  @parsed_file
end