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.



9655
9656
9657
9658
9659
9660
# File 'lib/syntax_tree/node.rb', line 9655

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

Instance Attribute Details

#bodystmtObject (readonly)

BodyStmt

the expressions to be executed



9650
9651
9652
# File 'lib/syntax_tree/node.rb', line 9650

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



9653
9654
9655
# File 'lib/syntax_tree/node.rb', line 9653

def comments
  @comments
end

#targetObject (readonly)

untyped

the target of the singleton class to enter



9647
9648
9649
# File 'lib/syntax_tree/node.rb', line 9647

def target
  @target
end

Instance Method Details

#===(other) ⇒ Object



9706
9707
9708
9709
# File 'lib/syntax_tree/node.rb', line 9706

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

#accept(visitor) ⇒ Object



9662
9663
9664
# File 'lib/syntax_tree/node.rb', line 9662

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

#child_nodesObject Also known as: deconstruct



9666
9667
9668
# File 'lib/syntax_tree/node.rb', line 9666

def child_nodes
  [target, bodystmt]
end

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



9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
# File 'lib/syntax_tree/node.rb', line 9670

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



9684
9685
9686
9687
9688
9689
9690
9691
# File 'lib/syntax_tree/node.rb', line 9684

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

#format(q) ⇒ Object



9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
# File 'lib/syntax_tree/node.rb', line 9693

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