Class: Lkml::Tree::ContainerNode
- Inherits:
-
SyntaxNode
- Object
- SyntaxNode
- Lkml::Tree::ContainerNode
- Defined in:
- lib/lkml/tree.rb
Instance Attribute Summary collapse
-
#items ⇒ Object
readonly
Returns the value of attribute items.
-
#top_level ⇒ Object
readonly
Returns the value of attribute top_level.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #accept(visitor) ⇒ Object
- #children ⇒ Object
-
#initialize(items:, top_level: false) ⇒ ContainerNode
constructor
A new instance of ContainerNode.
- #inspect ⇒ Object
- #line_number ⇒ Object
- #to_s ⇒ Object
- #validate_duplicate_keys! ⇒ Object
- #with(**changes) ⇒ Object
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
#items ⇒ Object (readonly)
Returns the value of attribute items.
294 295 296 |
# File 'lib/lkml/tree.rb', line 294 def items @items end |
#top_level ⇒ Object (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 |
#children ⇒ Object
320 321 322 |
# File 'lib/lkml/tree.rb', line 320 def children @items end |
#inspect ⇒ Object
304 305 306 |
# File 'lib/lkml/tree.rb', line 304 def inspect "#{self.class.name.split('::').last}()" end |
#line_number ⇒ Object
324 325 326 |
# File 'lib/lkml/tree.rb', line 324 def line_number @items[0]&.line_number end |
#to_s ⇒ Object
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 |