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, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

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

Returns a new instance of SClass.



9786
9787
9788
9789
9790
9791
# File 'lib/syntax_tree/node.rb', line 9786

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

Instance Attribute Details

#bodystmtObject (readonly)

BodyStmt

the expressions to be executed



9781
9782
9783
# File 'lib/syntax_tree/node.rb', line 9781

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



9784
9785
9786
# File 'lib/syntax_tree/node.rb', line 9784

def comments
  @comments
end

#targetObject (readonly)

Node

the target of the singleton class to enter



9778
9779
9780
# File 'lib/syntax_tree/node.rb', line 9778

def target
  @target
end

Instance Method Details

#===(other) ⇒ Object



9837
9838
9839
9840
# File 'lib/syntax_tree/node.rb', line 9837

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

#accept(visitor) ⇒ Object



9793
9794
9795
# File 'lib/syntax_tree/node.rb', line 9793

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

#child_nodesObject Also known as: deconstruct



9797
9798
9799
# File 'lib/syntax_tree/node.rb', line 9797

def child_nodes
  [target, bodystmt]
end

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



9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
# File 'lib/syntax_tree/node.rb', line 9801

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



9815
9816
9817
9818
9819
9820
9821
9822
# File 'lib/syntax_tree/node.rb', line 9815

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

#format(q) ⇒ Object



9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
# File 'lib/syntax_tree/node.rb', line 9824

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