Class: Lkml::Tree::ContainerNode

Inherits:
SyntaxNode show all
Defined in:
lib/lkml/tree.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(items:, top_level: false) ⇒ ContainerNode

Returns a new instance of ContainerNode.



296
297
298
299
300
301
302
# File 'lib/lkml/tree.rb', line 296

def initialize(items:, top_level: false)
  super()
  @top_level = top_level
  @items = items.freeze
  validate_duplicate_keys!
  freeze
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



294
295
296
# File 'lib/lkml/tree.rb', line 294

def items
  @items
end

#top_levelObject (readonly)

Returns the value of attribute top_level.



294
295
296
# File 'lib/lkml/tree.rb', line 294

def top_level
  @top_level
end

Instance Method Details

#==(other) ⇒ Object



343
344
345
346
347
# File 'lib/lkml/tree.rb', line 343

def ==(other)
  instance_of?(other.class) &&
    @top_level == other.top_level &&
    @items == other.items
end

#accept(visitor) ⇒ Object



328
329
330
# File 'lib/lkml/tree.rb', line 328

def accept(visitor)
  visitor.visit_container(self)
end

#childrenObject



320
321
322
# File 'lib/lkml/tree.rb', line 320

def children
  @items
end

#inspectObject



304
305
306
# File 'lib/lkml/tree.rb', line 304

def inspect
  "#{self.class.name.split('::').last}()"
end

#line_numberObject



324
325
326
# File 'lib/lkml/tree.rb', line 324

def line_number
  @items[0]&.line_number
end

#to_sObject



332
333
334
# File 'lib/lkml/tree.rb', line 332

def to_s
  Tree.items_to_str(*@items)
end

#validate_duplicate_keys!Object



308
309
310
311
312
313
314
315
316
317
318
# File 'lib/lkml/tree.rb', line 308

def validate_duplicate_keys!
  counts = Hash.new(0)
  @items.each { |item| counts[item.type.value] += 1 }
  counts.each do |key, count|
    next if @top_level || count <= 1 || Keys::PLURAL_KEYS.include?(key)

    raise KeyError,
          "Key \"#{key}\" already exists in tree and would overwrite the " \
          "existing value."
  end
end

#with(**changes) ⇒ Object



336
337
338
339
340
341
# File 'lib/lkml/tree.rb', line 336

def with(**changes)
  ContainerNode.new(
    items: changes.fetch(:items, @items),
    top_level: changes.fetch(:top_level, @top_level)
  )
end