Class: RBI::Method

Inherits:
NodeWithComments show all
Extended by:
T::Sig
Includes:
Indexable
Defined in:
lib/rbi/model.rb,
lib/rbi/index.rb,
lib/rbi/printer.rb,
lib/rbi/rewriters/merge_trees.rb

Overview

Methods and args

Instance Attribute Summary collapse

Attributes inherited from NodeWithComments

#comments

Attributes inherited from Node

#loc, #parent_tree

Instance Method Summary collapse

Methods inherited from Node

#detach, #group_kind, #parent_conflict_tree, #parent_scope, #print, #replace, #string

Constructor Details

#initialize(name, params: [], is_singleton: false, visibility: Visibility::Public, sigs: [], loc: nil, comments: [], &block) ⇒ Method

Returns a new instance of Method.



389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/rbi/model.rb', line 389

def initialize(
  name,
  params: [],
  is_singleton: false,
  visibility: Visibility::Public,
  sigs: [],
  loc: nil,
  comments: [],
  &block
)
  super(loc: loc, comments: comments)
  @name = name
  @params = params
  @is_singleton = is_singleton
  @visibility = visibility
  @sigs = sigs
  block&.call(self)
end

Instance Attribute Details

#is_singletonObject

Returns the value of attribute is_singleton.



369
370
371
# File 'lib/rbi/model.rb', line 369

def is_singleton
  @is_singleton
end

#nameObject

Returns the value of attribute name.



363
364
365
# File 'lib/rbi/model.rb', line 363

def name
  @name
end

#paramsObject (readonly)

Returns the value of attribute params.



366
367
368
# File 'lib/rbi/model.rb', line 366

def params
  @params
end

#sigsObject

Returns the value of attribute sigs.



375
376
377
# File 'lib/rbi/model.rb', line 375

def sigs
  @sigs
end

#visibilityObject

Returns the value of attribute visibility.



372
373
374
# File 'lib/rbi/model.rb', line 372

def visibility
  @visibility
end

Instance Method Details

#<<(param) ⇒ Object



409
410
411
# File 'lib/rbi/model.rb', line 409

def <<(param)
  @params << param
end

#accept_printer(v) ⇒ Object



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/rbi/printer.rb', line 310

def accept_printer(v)
  previous_node = v.previous_node
  v.printn if previous_node && (!previous_node.oneline? || !oneline?)

  v.visit_all(comments)
  v.visit_all(sigs)
  v.printl("# #{loc}") if loc && v.print_locs
  v.printt
  unless v.in_visibility_group || visibility == Visibility::Public
    v.print(visibility.visibility.to_s)
    v.print(" ")
  end
  v.print("def ")
  v.print("self.") if is_singleton
  v.print(name)
  unless params.empty?
    v.print("(")
    if inline_params?
      params.each_with_index do |param, index|
        v.print(", ") if index > 0
        v.visit(param)
      end
    else
      v.printn
      v.indent
      params.each_with_index do |param, pindex|
        v.printt
        v.visit(param)
        v.print(",") if pindex < params.size - 1
        param.comments.each_with_index do |comment, cindex|
          if cindex > 0
            param.print_comment_leading_space(v)
          else
            v.print(" ")
          end
          v.print("# #{comment.text.strip}")
        end
        v.printn
      end
      v.dedent
    end
    v.print(")")
  end
  v.print("; end")
  v.printn
end

#compatible_with?(other) ⇒ Boolean

Returns:

  • (Boolean)


389
390
391
392
393
394
# File 'lib/rbi/rewriters/merge_trees.rb', line 389

def compatible_with?(other)
  return false unless other.is_a?(Method)
  return false unless name == other.name
  return false unless params == other.params
  sigs.empty? || other.sigs.empty? || sigs == other.sigs
end

#fully_qualified_nameObject



414
415
416
417
418
419
420
# File 'lib/rbi/model.rb', line 414

def fully_qualified_name
  if is_singleton
    "#{parent_scope&.fully_qualified_name}::#{name}"
  else
    "#{parent_scope&.fully_qualified_name}##{name}"
  end
end

#index_idsObject



112
113
114
# File 'lib/rbi/index.rb', line 112

def index_ids
  [fully_qualified_name]
end

#inline_params?Boolean

Returns:

  • (Boolean)


363
364
365
# File 'lib/rbi/printer.rb', line 363

def inline_params?
  params.all? { |p| p.comments.empty? }
end

#merge_with(other) ⇒ Object



397
398
399
400
401
402
403
# File 'lib/rbi/rewriters/merge_trees.rb', line 397

def merge_with(other)
  return unless other.is_a?(Method)
  super
  other.sigs.each do |sig|
    sigs << sig unless sigs.include?(sig)
  end
end

#oneline?Boolean

Returns:

  • (Boolean)


358
359
360
# File 'lib/rbi/printer.rb', line 358

def oneline?
  comments.empty? && sigs.empty? && inline_params?
end

#to_sObject



423
424
425
# File 'lib/rbi/model.rb', line 423

def to_s
  "#{fully_qualified_name}(#{params.join(", ")})"
end