File: path_builder.rb

package info (click to toggle)
ruby-hocon 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 768 kB
  • sloc: ruby: 7,903; makefile: 4
file content (56 lines) | stat: -rw-r--r-- 1,055 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
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
50
51
52
53
54
55
56
# encoding: utf-8

require_relative '../../hocon/impl'
require_relative '../../hocon/impl/path'
require_relative '../../hocon/config_error'

class Hocon::Impl::PathBuilder

  def initialize
    @keys = []
    @result = nil
  end

  def check_can_append
    if @result
      raise Hocon::ConfigError::ConfigBugOrBrokenError, "Adding to PathBuilder after getting result"
    end
  end

  def append_key(key)
    check_can_append
    @keys.push(key)
  end

  def append_path(path)
    check_can_append

    first = path.first
    remainder = path.remainder

    loop do
      @keys.push(first)

      if !remainder.nil?
        first = remainder.first
        remainder = remainder.remainder
      else
        break
      end
    end
  end

  def result
    # note: if keys is empty, we want to return nil, which is a valid
    # empty path
    if @result.nil?
      remainder = nil
      while !@keys.empty?
        key = @keys.pop
        remainder = Hocon::Impl::Path.new(key, remainder)
      end
      @result = remainder
    end
    @result
  end
end