Class: Hocon::Impl::SimpleConfigList

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
ConfigList, AbstractConfigValue, Container
Defined in:
lib/hocon/impl/simple_config_list.rb

Defined Under Namespace

Classes: ResolveModifier

Constant Summary collapse

ResolveStatus =
Hocon::Impl::ResolveStatus
ResolveResult =
Hocon::Impl::ResolveResult
ConfigBugOrBrokenError =
Hocon::ConfigError::ConfigBugOrBrokenError

Constants included from AbstractConfigValue

AbstractConfigValue::ConfigImplUtil

Instance Attribute Summary collapse

Attributes included from AbstractConfigValue

#origin

Instance Method Summary collapse

Methods included from AbstractConfigValue

#at_key, #at_key_with_origin, #at_path, #at_path_with_origin, #construct_delayed_merge, #delay_merge, has_descendant_in_list?, #ignores_fallbacks?, indent, #inspect, #merged_stack_with_non_object, #merged_stack_with_object, #merged_stack_with_the_unmergeable, #merged_with_non_object, #merged_with_object, #merged_with_the_unmergeable, #render, #render_to_sb, replace_child_in_list, #require_not_ignoring_fallbacks, #to_fallback_value, #to_s, #transform_to_string, #with_fallback, #with_fallbacks_ignored

Methods included from ConfigValue

#at_key, #at_path, #origin, #render, #with_fallback

Methods included from ConfigMergeable

#with_fallback

Constructor Details

#initialize(origin, value, status = ResolveStatus.from_values(value)) ⇒ SimpleConfigList

Returns a new instance of SimpleConfigList.



24
25
26
27
28
29
30
31
32
33
# File 'lib/hocon/impl/simple_config_list.rb', line 24

def initialize(origin, value, status = ResolveStatus.from_values(value))
  super(origin)
  @value = value
  @resolved = (status == ResolveStatus::RESOLVED)

  # kind of an expensive debug check (makes this constructor pointless)
  if status != ResolveStatus.from_values(value)
    raise ConfigBugOrBrokenError, "SimpleConfigList created with wrong resolve status: #{self}"
  end
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



35
36
37
# File 'lib/hocon/impl/simple_config_list.rb', line 35

def value
  @value
end

Instance Method Details

#<<(e) ⇒ Object



329
330
331
# File 'lib/hocon/impl/simple_config_list.rb', line 329

def <<(e)
  raise we_are_immutable("<<")
end

#==(other) ⇒ Object



168
169
170
171
172
173
174
175
176
177
# File 'lib/hocon/impl/simple_config_list.rb', line 168

def ==(other)
  # note that "origin" is deliberately NOT part of equality
  if other.is_a?(self.class)
    # optimization to avoid unwrapped() for two ConfigList
    can_equal(other) &&
        (value.equal?(other.value) || (value == other.value))
  else
    false
  end
end

#[]=(index, element) ⇒ Object



321
322
323
# File 'lib/hocon/impl/simple_config_list.rb', line 321

def []=(index, element)
  raise we_are_immutable("[]=")
end

#add(e) ⇒ Object



277
278
279
# File 'lib/hocon/impl/simple_config_list.rb', line 277

def add(e)
  raise we_are_immutable("add")
end

#add_all(c) ⇒ Object



285
286
287
# File 'lib/hocon/impl/simple_config_list.rb', line 285

def add_all(c)
  raise we_are_immutable("add_all")
end

#add_all_at(index, c) ⇒ Object



289
290
291
# File 'lib/hocon/impl/simple_config_list.rb', line 289

def add_all_at(index, c)
  raise we_are_immutable("add_all_at")
end

#add_at(index, element) ⇒ Object



281
282
283
# File 'lib/hocon/impl/simple_config_list.rb', line 281

def add_at(index, element)
  raise we_are_immutable("add_at")
end

#can_equal(other) ⇒ Object



164
165
166
# File 'lib/hocon/impl/simple_config_list.rb', line 164

def can_equal(other)
  other.is_a?(self.class)
end

#clearObject



293
294
295
# File 'lib/hocon/impl/simple_config_list.rb', line 293

def clear
  raise we_are_immutable("clear")
end

#concatenate(other) ⇒ Object



337
338
339
340
341
# File 'lib/hocon/impl/simple_config_list.rb', line 337

def concatenate(other)
  combined_origin = Hocon::Impl::SimpleConfigOrigin.merge_two_origins(origin, other.origin)
  combined = value + other.value
  Hocon::Impl::SimpleConfigList.new(combined_origin, combined)
end

#contains?(o) ⇒ Boolean

Returns:

  • (Boolean)


231
232
233
# File 'lib/hocon/impl/simple_config_list.rb', line 231

def contains?(o)
  value.include?(o)
end

#contains_all?(c) ⇒ Boolean

Returns:

  • (Boolean)


239
240
241
# File 'lib/hocon/impl/simple_config_list.rb', line 239

def contains_all?(c)
  include_all?(c)
end

#delete(o) ⇒ Object



305
306
307
# File 'lib/hocon/impl/simple_config_list.rb', line 305

def delete(o)
  raise we_are_immutable("delete")
end

#get(index) ⇒ Object



243
244
245
# File 'lib/hocon/impl/simple_config_list.rb', line 243

def get(index)
  value[index]
end

#has_descendant?(descendant) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/hocon/impl/simple_config_list.rb', line 61

def has_descendant?(descendant)
  Hocon::Impl::AbstractConfigValue.has_descendant_in_list?(@value, descendant)
end

#hashObject



179
180
181
182
# File 'lib/hocon/impl/simple_config_list.rb', line 179

def hash
  # note that "origin" is deliberately NOT part of equality
  value.hash
end

#include_all?(value_list) ⇒ Boolean

Returns:

  • (Boolean)


235
236
237
# File 'lib/hocon/impl/simple_config_list.rb', line 235

def include_all?(value_list)
  value_list.all? { |v| @value.include?(v)}
end

#index_of(o) ⇒ Object



247
248
249
# File 'lib/hocon/impl/simple_config_list.rb', line 247

def index_of(o)
  value.index(o)
end

#is_emptyObject



251
252
253
# File 'lib/hocon/impl/simple_config_list.rb', line 251

def is_empty
  empty?
end

#last_index_of(o) ⇒ Object

Skipping upstream definition of “iterator”, because that’s not really a thing in Ruby.



258
259
260
# File 'lib/hocon/impl/simple_config_list.rb', line 258

def last_index_of(o)
  value.rindex(o)
end

#modify(modifier, new_resolve_status) ⇒ Object



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

def modify(modifier, new_resolve_status)
  begin
    modify_may_throw(modifier, new_resolve_status)
  rescue Hocon::ConfigError => e
    raise e
  end
end

#modify_may_throw(modifier, new_resolve_status) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/hocon/impl/simple_config_list.rb', line 73

def modify_may_throw(modifier, new_resolve_status)
  # lazy-create for optimization
  changed = nil
  i = 0
  @value.each { |v|
    modified = modifier.modify_child_may_throw(nil, v)

    # lazy-create the new list if required
    if changed == nil && !modified.equal?(v)
      changed = []
      j = 0
      while j < i
        changed << @value[j]
        j += 1
      end
    end

    # once the new list is created, all elements
    # have to go in it.if modifyChild returned
    # null, we drop that element.
    if changed != nil && modified != nil
      changed << modified
    end

    i += 1
  }

  if changed != nil
    if new_resolve_status != nil
      self.class.new(origin, changed, new_resolve_status)
    else
      self.class.new(origin, changed)
    end
  else
    self
  end
end

#new_copy(origin) ⇒ Object



333
334
335
# File 'lib/hocon/impl/simple_config_list.rb', line 333

def new_copy(origin)
  Hocon::Impl::SimpleConfigList.new(origin, @value)
end

#push(e) ⇒ Object



325
326
327
# File 'lib/hocon/impl/simple_config_list.rb', line 325

def push(e)
  raise we_are_immutable("push")
end

#relativized(prefix) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/hocon/impl/simple_config_list.rb', line 149

def relativized(prefix)
  modifier = Class.new do
    include Hocon::Impl::AbstractConfigValue::NoExceptionsModifier

    # prefix isn't in scope inside of a def, but it is in scope inside of Class.new
    # so manually define a method that has access to prefix
    # I feel dirty
    define_method(:modify_child) do |key, v|
      v.relativized(prefix)
    end
  end

  modify(modifier.new, resolve_status)
end

#remove(o) ⇒ Object



297
298
299
# File 'lib/hocon/impl/simple_config_list.rb', line 297

def remove(o)
  raise we_are_immutable("remove")
end

#remove_all(c) ⇒ Object



309
310
311
# File 'lib/hocon/impl/simple_config_list.rb', line 309

def remove_all(c)
  raise we_are_immutable("remove_all")
end

#remove_at(i) ⇒ Object



301
302
303
# File 'lib/hocon/impl/simple_config_list.rb', line 301

def remove_at(i)
  raise we_are_immutable("remove_at")
end

#render_value_to_sb(sb, indent_size, at_root, options) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/hocon/impl/simple_config_list.rb', line 184

def render_value_to_sb(sb, indent_size, at_root, options)
  if @value.empty?
    sb << "[]"
  else
    sb << "["
    if options.formatted?
      sb << "\n"
    end
    @value.each do |v|
      if options.origin_comments?
        lines = v.origin.description.split("\n")
        lines.each do |l|
          Hocon::Impl::AbstractConfigValue.indent(sb, indent_size + 1, options)
          sb << "# "
          sb << l
          sb << "\n"
        end
      end
      if options.comments?
        v.origin.comments.each do |comment|
          sb << "# "
          sb << comment
          sb << "\n"
        end
      end
      Hocon::Impl::AbstractConfigValue.indent(sb, indent_size + 1, options)

      v.render_value_to_sb(sb, indent_size + 1, at_root, options)
      sb << ","
      if options.formatted?
        sb << "\n"
      end
    end

    # couldn't figure out a better way to chop characters off of the end of
    # the StringIO.  This relies on making sure that, prior to returning the
    # final string, we take a substring that ends at sb.pos.
    sb.pos = sb.pos - 1 # chop or newline
    if options.formatted?
      sb.pos = sb.pos - 1 # also chop comma
      sb << "\n"
      Hocon::Impl::AbstractConfigValue.indent(sb, indent_size, options)
    end
    sb << "]"
  end
end

#replace_child(child, replacement) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/hocon/impl/simple_config_list.rb', line 51

def replace_child(child, replacement)
  new_list = replace_child_in_list(@value, child, replacement)
  if new_list.nil?
    nil
  else
    # we use the constructor flavor that will recompute the resolve status
    SimpleConfigList.new(origin, new_list)
  end
end

#resolve_statusObject



47
48
49
# File 'lib/hocon/impl/simple_config_list.rb', line 47

def resolve_status
  ResolveStatus.from_boolean(@resolved)
end

#resolve_substitutions(context, source) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/hocon/impl/simple_config_list.rb', line 125

def resolve_substitutions(context, source)
  if @resolved
    return Hocon::Impl::ResolveResult.make(context, self)
  end

  if context.is_restricted_to_child
    # if a list restricts to a child path, then it has no child paths,
    # so nothing to do.
    Hocon::Impl::ResolveResult.make(context, self)
  else
    begin
      modifier = ResolveModifier.new(context, source.push_parent(self))
      value = modify_may_throw(modifier, context.options.allow_unresolved ? nil : ResolveStatus::RESOLVED)
      Hocon::Impl::ResolveResult.make(modifier.context, value)
    rescue NotPossibleToResolve => e
      raise e
    rescue RuntimeError => e
      raise e
    rescue Exception => e
      raise ConfigBugOrBrokenError.new("unexpected exception", e)
    end
  end
end

#retain_all(c) ⇒ Object



313
314
315
# File 'lib/hocon/impl/simple_config_list.rb', line 313

def retain_all(c)
  raise we_are_immutable("retain_all")
end

#set(index, element) ⇒ Object



317
318
319
# File 'lib/hocon/impl/simple_config_list.rb', line 317

def set(index, element)
  raise we_are_immutable("set")
end

#sub_list(from_index, to_index) ⇒ Object

skipping upstream definitions of “wrapListIterator”, “listIterator”, and “listIterator(int)”, because those don’t really apply in Ruby.



265
266
267
# File 'lib/hocon/impl/simple_config_list.rb', line 265

def sub_list(from_index, to_index)
  value[from_index..to_index]
end

#to_arrayObject



269
270
271
# File 'lib/hocon/impl/simple_config_list.rb', line 269

def to_array
  value
end

#unwrappedObject



43
44
45
# File 'lib/hocon/impl/simple_config_list.rb', line 43

def unwrapped
  @value.map { |v| v.unwrapped }
end

#value_typeObject



39
40
41
# File 'lib/hocon/impl/simple_config_list.rb', line 39

def value_type
  Hocon::ConfigValueType::LIST
end

#we_are_immutable(method) ⇒ Object



273
274
275
# File 'lib/hocon/impl/simple_config_list.rb', line 273

def we_are_immutable(method)
  Hocon::Impl::UnsupportedOperationError.new("ConfigList is immutable, you can't call List. '#{method}'")
end

#with_origin(origin) ⇒ Object

Skipping upstream “writeReplace” until we see that we need it for something



345
346
347
# File 'lib/hocon/impl/simple_config_list.rb', line 345

def with_origin(origin)
  super(origin)
end