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 NodeWithComments

#annotations

Methods inherited from Node

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

Constructor Details

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

Returns a new instance of Method.



505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
# File 'lib/rbi/model.rb', line 505

def initialize(
  name,
  params: [],
  is_singleton: false,
  visibility: Public.new,
  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.



485
486
487
# File 'lib/rbi/model.rb', line 485

def is_singleton
  @is_singleton
end

#nameObject

Returns the value of attribute name.



479
480
481
# File 'lib/rbi/model.rb', line 479

def name
  @name
end

#paramsObject (readonly)

Returns the value of attribute params.



482
483
484
# File 'lib/rbi/model.rb', line 482

def params
  @params
end

#sigsObject

Returns the value of attribute sigs.



491
492
493
# File 'lib/rbi/model.rb', line 491

def sigs
  @sigs
end

#visibilityObject

Returns the value of attribute visibility.



488
489
490
# File 'lib/rbi/model.rb', line 488

def visibility
  @visibility
end

Instance Method Details

#<<(param) ⇒ Object



525
526
527
# File 'lib/rbi/model.rb', line 525

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

#accept_printer(v) ⇒ Object



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/rbi/printer.rb', line 354

def accept_printer(v)
  print_blank_line_before(v)

  v.visit_all(comments)
  v.visit_all(sigs)
  v.printl("# #{loc}") if loc && v.print_locs
  v.printt
  unless v.in_visibility_group || 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_lines.each_with_index do |comment, cindex|
          if cindex > 0
            param.print_comment_leading_space(v, last: pindex == params.size - 1)
          else
            v.print(" ")
          end
          v.print("# #{comment}")
        end
        v.printn
      end
      v.dedent
    end
    v.print(")")
  end
  v.print("; end")
  v.printn
end

#compatible_with?(other) ⇒ Boolean

Returns:

  • (Boolean)


453
454
455
456
457
458
# File 'lib/rbi/rewriters/merge_trees.rb', line 453

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



530
531
532
533
534
535
536
# File 'lib/rbi/model.rb', line 530

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

#index_idsObject



119
120
121
# File 'lib/rbi/index.rb', line 119

def index_ids
  [fully_qualified_name]
end

#inline_params?Boolean

Returns:

  • (Boolean)


407
408
409
# File 'lib/rbi/printer.rb', line 407

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

#merge_with(other) ⇒ Object



461
462
463
464
465
466
467
# File 'lib/rbi/rewriters/merge_trees.rb', line 461

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)


402
403
404
# File 'lib/rbi/printer.rb', line 402

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

#to_sObject



539
540
541
# File 'lib/rbi/model.rb', line 539

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