Class: SyntaxTree::SClass

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

SClass represents a block of statements that should be evaluated within the context of the singleton class of an object. It’s frequently used to define singleton methods.

class << self
end

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #pretty_print, #to_json

Constructor Details

#initialize(target:, bodystmt:, location:) ⇒ SClass

Returns a new instance of SClass.



9690
9691
9692
9693
9694
9695
# File 'lib/syntax_tree/node.rb', line 9690

def initialize(target:, bodystmt:, location:)
  @target = target
  @bodystmt = bodystmt
  @location = location
  @comments = []
end

Instance Attribute Details

#bodystmtObject (readonly)

BodyStmt

the expressions to be executed



9685
9686
9687
# File 'lib/syntax_tree/node.rb', line 9685

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



9688
9689
9690
# File 'lib/syntax_tree/node.rb', line 9688

def comments
  @comments
end

#targetObject (readonly)

untyped

the target of the singleton class to enter



9682
9683
9684
# File 'lib/syntax_tree/node.rb', line 9682

def target
  @target
end

Instance Method Details

#===(other) ⇒ Object



9741
9742
9743
9744
# File 'lib/syntax_tree/node.rb', line 9741

def ===(other)
  other.is_a?(SClass) && target === other.target &&
    bodystmt === other.bodystmt
end

#accept(visitor) ⇒ Object



9697
9698
9699
# File 'lib/syntax_tree/node.rb', line 9697

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

#child_nodesObject Also known as: deconstruct



9701
9702
9703
# File 'lib/syntax_tree/node.rb', line 9701

def child_nodes
  [target, bodystmt]
end

#copy(target: nil, bodystmt: nil, location: nil) ⇒ Object



9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
# File 'lib/syntax_tree/node.rb', line 9705

def copy(target: nil, bodystmt: nil, location: nil)
  node =
    SClass.new(
      target: target || self.target,
      bodystmt: bodystmt || self.bodystmt,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



9719
9720
9721
9722
9723
9724
9725
9726
# File 'lib/syntax_tree/node.rb', line 9719

def deconstruct_keys(_keys)
  {
    target: target,
    bodystmt: bodystmt,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
# File 'lib/syntax_tree/node.rb', line 9728

def format(q)
  q.text("class << ")
  q.group do
    q.format(target)
    q.indent do
      q.breakable_force
      q.format(bodystmt)
    end
    q.breakable_force
  end
  q.text("end")
end