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.



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

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

Instance Attribute Details

#bodystmtObject (readonly)

BodyStmt

the expressions to be executed



9659
9660
9661
# File 'lib/syntax_tree/node.rb', line 9659

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



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

def comments
  @comments
end

#targetObject (readonly)

untyped

the target of the singleton class to enter



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

def target
  @target
end

Instance Method Details

#===(other) ⇒ Object



9715
9716
9717
9718
# File 'lib/syntax_tree/node.rb', line 9715

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

#accept(visitor) ⇒ Object



9671
9672
9673
# File 'lib/syntax_tree/node.rb', line 9671

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

#child_nodesObject Also known as: deconstruct



9675
9676
9677
# File 'lib/syntax_tree/node.rb', line 9675

def child_nodes
  [target, bodystmt]
end

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



9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
# File 'lib/syntax_tree/node.rb', line 9679

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



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

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

#format(q) ⇒ Object



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

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