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.



13
14
15
16
17
18
# File 'lib/hocon/impl/path.rb', line 13

def initialize(first, remainder)
  # first: String, remainder: Path

  @first = first
  @remainder = remainder
end

Instance Attribute Details

#firstObject (readonly)

Returns the value of attribute first.



19
20
21
# File 'lib/hocon/impl/path.rb', line 19

def first
  @first
end

#remainderObject (readonly)

Returns the value of attribute remainder.



19
20
21
# File 'lib/hocon/impl/path.rb', line 19

def remainder
  @remainder
end

Class Method Details

.from_path_iterator(path_iterator) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/hocon/impl/path.rb', line 59

def self.from_path_iterator(path_iterator)
  # This method was translated from the Path constructors in the
  # Java hocon library that takes in an iterator of Paths
  #
  # It figures out what @first and @remainder should be, then
  # pass those to the ruby constructor

  # Try to get first path from iterator
  # Ruby iterators have no .hasNext() method like java
  # So we try to catch the StopIteration exception
  begin
    first_path = path_iterator.next
  rescue StopIteration
    raise Hocon::ConfigError::ConfigBugOrBrokenError("empty path")
  end

  new_first = first_path.first

  pb = Hocon::Impl::PathBuilder.new

  unless first_path.remainder.nil?
    pb.append_path(first_path.remainder)
  end

  # Skip first path
  path_iterator.drop(1).each do |path|
    pb.append_path(path)
  end

  new_remainder = pb.result

  self.new(new_first, new_remainder)
end

.from_path_list(path_list) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/hocon/impl/path.rb', line 50

def self.from_path_list(path_list)
  # This method was translated from the Path constructors in the
  # Java hocon library that take in a list of Paths
  #
  # It just passes an iterator to self.from_path_iterator, which
  # will return a new Path object
  from_path_iterator(path_list.each)
end

.from_string_list(elements) ⇒ Object



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
# File 'lib/hocon/impl/path.rb', line 21

def self.from_string_list(elements)
  # This method was translated from the Path constructor in the
  # Java hocon library that has this signature:
  #   Path(String... elements)
  #
  # It figures out what @first and @remainder should be, then
  # pass those to the ruby constructor
  if elements.length == 0
    raise Hocon::ConfigError::ConfigBugOrBrokenError.new("empty path")
  end

  new_first = elements.first

  if elements.length > 1
    pb = Hocon::Impl::PathBuilder.new

    # Skip first element
    elements.drop(1).each do |element|
      pb.append_key(element)
    end

    new_remainder = pb.result
  else
    new_remainder = nil
  end

  self.new(new_first, new_remainder)
end

.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)


204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/hocon/impl/path.rb', line 204

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

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

  false
end

.new_key(key) ⇒ Object



255
256
257
# File 'lib/hocon/impl/path.rb', line 255

def self.new_key(key)
  return self.new(key, nil)
end

.new_path(path) ⇒ Object



259
260
261
# File 'lib/hocon/impl/path.rb', line 259

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

Instance Method Details

#==(other) ⇒ Object



187
188
189
190
191
192
193
194
# File 'lib/hocon/impl/path.rb', line 187

def ==(other)
  if other.is_a? Hocon::Impl::Path
    that = other
    first == that.first && ConfigImplUtil.equals_handling_nil?(remainder, that.remainder)
  else
    false
  end
end

#append_to_string_builder(sb) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/hocon/impl/path.rb', line 219

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

#hashObject



196
197
198
199
200
# File 'lib/hocon/impl/path.rb', line 196

def hash
  remainder_hash = remainder.nil? ? 0 : remainder.hash

  41 * (41 + first.hash) + remainder_hash
end

#inspectObject



241
242
243
# File 'lib/hocon/impl/path.rb', line 241

def inspect
  to_s
end

#lastObject



115
116
117
118
119
120
121
# File 'lib/hocon/impl/path.rb', line 115

def last
  p = self
  while p.remainder != nil
    p = p.remainder
  end
  p.first
end

#lengthObject



132
133
134
135
136
137
138
139
140
# File 'lib/hocon/impl/path.rb', line 132

def length
  count = 1
  p = remainder
  while p != nil do
    count += 1
    p = p.remainder
  end
  count
end

#parentObject



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/hocon/impl/path.rb', line 101

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

#prepend(to_prepend) ⇒ Object



123
124
125
126
127
128
129
130
# File 'lib/hocon/impl/path.rb', line 123

def prepend(to_prepend)
  pb = Hocon::Impl::PathBuilder.new

  pb.append_path(to_prepend)
  pb.append_path(self)

  pb.result
end

#renderObject

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



249
250
251
252
253
# File 'lib/hocon/impl/path.rb', line 249

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

#starts_with(other) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/hocon/impl/path.rb', line 171

def starts_with(other)
  my_remainder = self
  other_remainder = other
  if other_remainder.length <= my_remainder.length
    while ! other_remainder.nil?
      if ! (other_remainder.first == my_remainder.first)
        return false
      end
      my_remainder = my_remainder.remainder
      other_remainder = other_remainder.remainder
    end
    return true
  end
  false
end

#sub_path(first_index, last_index) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/hocon/impl/path.rb', line 142

def sub_path(first_index, last_index)
  if last_index < first_index
    raise ConfigBugOrBrokenError.new("bad call to sub_path")
  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}")
    end
  end
  pb.result
end

#sub_path_to_end(remove_from_front) ⇒ Object

translated from ‘subPath(int removeFromFront)` upstream



161
162
163
164
165
166
167
168
169
# File 'lib/hocon/impl/path.rb', line 161

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

#to_sObject



232
233
234
235
236
237
238
239
# File 'lib/hocon/impl/path.rb', line 232

def to_s
  sb = StringIO.new
  sb << "Path("
  append_to_string_builder(sb)
  sb << ")"

  sb.string
end