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.



513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
# File 'lib/rbi/model.rb', line 513

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.



493
494
495
# File 'lib/rbi/model.rb', line 493

def is_singleton
  @is_singleton
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#paramsObject (readonly)

Returns the value of attribute params.



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

def params
  @params
end

#sigsObject

Returns the value of attribute sigs.



499
500
501
# File 'lib/rbi/model.rb', line 499

def sigs
  @sigs
end

#visibilityObject

Returns the value of attribute visibility.



496
497
498
# File 'lib/rbi/model.rb', line 496

def visibility
  @visibility
end

Instance Method Details

#<<(param) ⇒ Object



533
534
535
# File 'lib/rbi/model.rb', line 533

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

#accept_printer(v) ⇒ Object



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/rbi/printer.rb', line 384

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)


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

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



538
539
540
541
542
543
544
# File 'lib/rbi/model.rb', line 538

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

#index_idsObject



123
124
125
# File 'lib/rbi/index.rb', line 123

def index_ids
  [fully_qualified_name]
end

#inline_params?Boolean

Returns:

  • (Boolean)


437
438
439
# File 'lib/rbi/printer.rb', line 437

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

#merge_with(other) ⇒ Object



470
471
472
473
474
475
476
477
# File 'lib/rbi/rewriters/merge_trees.rb', line 470

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)


432
433
434
# File 'lib/rbi/printer.rb', line 432

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

#to_sObject



547
548
549
# File 'lib/rbi/model.rb', line 547

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