Class: YARP::AssocNode

Inherits:
YARPNode
  • Object
show all
Defined in:
lib/yarp/node.rb,
ext/yarp/api_node.c

Overview

Represents a hash key/value pair.

{ a => b }
  ^^^^^^

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, value, operator_loc, location) ⇒ AssocNode

def initialize: (key: Node, value: Node?, operator_loc: Location?, location: Location) -> void



478
479
480
481
482
483
# File 'lib/yarp/node.rb', line 478

def initialize(key, value, operator_loc, location)
  @key = key
  @value = value
  @operator_loc = operator_loc
  @location = location
end

Instance Attribute Details

#keyObject (readonly)

attr_reader key: Node



469
470
471
# File 'lib/yarp/node.rb', line 469

def key
  @key
end

#operator_locObject (readonly)

attr_reader operator_loc: Location?



475
476
477
# File 'lib/yarp/node.rb', line 475

def operator_loc
  @operator_loc
end

#valueObject (readonly)

attr_reader value: Node?



472
473
474
# File 'lib/yarp/node.rb', line 472

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



486
487
488
# File 'lib/yarp/node.rb', line 486

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array[nil | Node]



491
492
493
# File 'lib/yarp/node.rb', line 491

def child_nodes
  [key, value]
end

#comment_targetsObject

def comment_targets: () -> Array[Node | Location]



496
497
498
# File 'lib/yarp/node.rb', line 496

def comment_targets
  [key, *value, *operator_loc]
end

#copy(**params) ⇒ Object

def copy: (**params) -> AssocNode



501
502
503
504
505
506
507
508
# File 'lib/yarp/node.rb', line 501

def copy(**params)
  AssocNode.new(
    params.fetch(:key) { key },
    params.fetch(:value) { value },
    params.fetch(:operator_loc) { operator_loc },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (keys: Array) -> Hash[Symbol, nil | Node | Array | String | Token | Array | Location]



514
515
516
# File 'lib/yarp/node.rb', line 514

def deconstruct_keys(keys)
  { key: key, value: value, operator_loc: operator_loc, location: location }
end

#inspect(inspector = NodeInspector.new) ⇒ Object



523
524
525
526
527
528
529
530
531
532
533
534
535
# File 'lib/yarp/node.rb', line 523

def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  inspector << "├── key:\n"
  inspector << inspector.child_node(key, "")
  if (value = self.value).nil?
    inspector << "├── value: ∅\n"
  else
    inspector << "├── value:\n"
    inspector << value.inspect(inspector.child_inspector("")).delete_prefix(inspector.prefix)
  end
  inspector << "└── operator_loc: #{inspector.location(operator_loc)}\n"
  inspector.to_str
end

#operatorObject

def operator: () -> String?



519
520
521
# File 'lib/yarp/node.rb', line 519

def operator
  operator_loc&.slice
end