File: config_node_path.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 (48 lines) | stat: -rw-r--r-- 1,177 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
# encoding: utf-8

require_relative '../../hocon/impl'
require_relative '../../hocon/impl/tokens'
require_relative '../../hocon/impl/abstract_config_node'

class Hocon::Impl::ConfigNodePath
  include Hocon::Impl::AbstractConfigNode
  Tokens = Hocon::Impl::Tokens

  def initialize(path, tokens)
    @path = path
    @tokens = tokens
  end

  attr_reader :tokens

  def value
    @path
  end

  def sub_path(to_remove)
    period_count = 0
    tokens_copy = tokens.clone
    (0..tokens_copy.size - 1).each do |i|
      if Tokens.unquoted_text?(tokens_copy[i]) &&
          tokens_copy[i].token_text == "."
        period_count += 1
      end

      if period_count == to_remove
        return self.class.new(@path.sub_path_to_end(to_remove), tokens_copy[i + 1..tokens_copy.size])
      end
    end
    raise ConfigBugOrBrokenError, "Tried to remove too many elements from a Path node"
  end

  def first
    tokens_copy = tokens.clone
    (0..tokens_copy.size - 1).each do |i|
      if Tokens.unquoted_text?(tokens_copy[i]) &&
          tokens_copy[i].token_text == "."
        return self.class.new(@path.sub_path(0, 1), tokens_copy[0, i])
      end
    end
    self
  end
end