Class: Hocon::Impl::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/hocon/impl/path.rb

Constant Summary collapse

ConfigBugOrBrokenError =
Hocon::ConfigError::ConfigBugOrBrokenError
ConfigImplUtil =
Hocon::Impl::ConfigImplUtil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(first, remainder) ⇒ Path

Returns a new instance of Path.



37
38
39
40
# File 'lib/hocon/impl/path.rb', line 37

def initialize(first, remainder)
  @first = first
  @remainder = remainder
end

Instance Attribute Details

#firstObject (readonly)

Returns the value of attribute first.



41
42
43
# File 'lib/hocon/impl/path.rb', line 41

def first
  @first
end

#remainderObject (readonly)

Returns the value of attribute remainder.



41
42
43
# File 'lib/hocon/impl/path.rb', line 41

def remainder
  @remainder
end

Class Method Details

.has_funky_chars?(s) ⇒ Boolean

this doesn’t have a very precise meaning, just to reduce noise from quotes in the rendered path for average cases

Returns:

  • (Boolean)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/hocon/impl/path.rb', line 13

def self.has_funky_chars?(s)
  length = s.length
  if length == 0
    return false
  end

  # if the path starts with something that could be a number,
  # we need to quote it because the number could be invalid,
  # for example it could be a hyphen with no digit afterward
  # or the exponent "e" notation could be mangled.
  first = s[0]
  unless first =~ /[[:alpha:]]/
    return true
  end

  s.chars.each do |c|
    unless (c =~ /[[:alnum:]]/) || (c == '-') || (c == '_')
      return true
    end
  end

  false
end

.new_path(path) ⇒ Object



134
135
136
# File 'lib/hocon/impl/path.rb', line 134

def self.new_path(path)
  Hocon::Impl::Parser.parse_path(path)
end

Instance Method Details

#append_to_string_builder(sb) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/hocon/impl/path.rb', line 93

def append_to_string_builder(sb)
  if self.class.has_funky_chars?(@first) || @first.empty?
    sb << ConfigImplUtil.render_json_string(@first)
  else
    sb << @first
  end

  unless @remainder.nil?
    sb << "."
    @remainder.append_to_string_builder(sb)
  end
end

#lastObject



65
66
67
68
69
70
71
# File 'lib/hocon/impl/path.rb', line 65

def last
  p = self
  while not p.remainder.nil?
    p = p.remainder
  end
  p.first
end

#lengthObject



73
74
75
76
77
78
79
80
81
# File 'lib/hocon/impl/path.rb', line 73

def length
  count = 1
  p = remainder
  while not p.nil? do
    count += 1
    p = p.remainder
  end
  return count
end

#parentObject



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/hocon/impl/path.rb', line 51

def parent
  if remainder.nil?
    return nil
  end

  pb = Hocon::Impl::PathBuilder.new
  p = self
  while not p.remainder.nil?
    pb.append_key(p.first)
    p = p.remainder
  end
  pb.result
end

#renderObject

toString() is a debugging-oriented version while this is an error-message-oriented human-readable one.



87
88
89
90
91
# File 'lib/hocon/impl/path.rb', line 87

def render
  sb = StringIO.new
  append_to_string_builder(sb)
  sb.string
end

#sub_path(first_index, last_index) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/hocon/impl/path.rb', line 116

def sub_path(first_index, last_index)
  if last_index < first_index
    raise ConfigBugOrBrokenError.new("bad call to sub_path", nil)
  end
  from = sub_path_to_end(first_index)
  pb = Hocon::Impl::PathBuilder.new
  count = last_index - first_index
  while count > 0 do
    count -= 1
    pb.append_key(from.first)
    from = from.remainder
    if from.nil?
      raise ConfigBugOrBrokenError.new("sub_path last_index out of range #{last_index}", nil)
    end
  end
  pb.result
end

#sub_path_to_end(remove_from_front) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'lib/hocon/impl/path.rb', line 106

def sub_path_to_end(remove_from_front)
  count = remove_from_front
  p = self
  while (not p.nil?) && count > 0 do
    count -= 1
    p = p.remainder
  end
  p
end