Class: RBI::Method

Inherits:
NodeWithComments show all
Includes:
Indexable
Defined in:
lib/rbi/model.rb,
lib/rbi/index.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, #comments?, #version_requirements

Methods inherited from Node

#detach, #parent_conflict_tree, #parent_scope, #print, #rbs_print, #rbs_string, #replace, #satisfies_version?, #string

Constructor Details

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

: ( | String name, | ?params: Array?, | ?is_singleton: bool, | ?visibility: Visibility, | ?sigs: Array?, | ?loc: Loc?, | ?comments: Array? | ) ?{ (Method node) -> void } -> void



491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
# File 'lib/rbi/model.rb', line 491

def initialize(
  name,
  params: nil,
  is_singleton: false,
  visibility: Public::DEFAULT,
  sigs: nil,
  loc: nil,
  comments: nil,
  &block
)
  super(loc: loc, comments: comments)
  @name = name.to_s #: String
  @params = params #: Array[Param]?
  @is_singleton = is_singleton
  @visibility = visibility
  @sigs = sigs #: Array[Sig]?
  block&.call(self)
end

Instance Attribute Details

#is_singletonObject

: bool



464
465
466
# File 'lib/rbi/model.rb', line 464

def is_singleton
  @is_singleton
end

#nameObject (readonly)

: String



456
457
458
# File 'lib/rbi/model.rb', line 456

def name
  @name
end

#sigsObject

: -> Array



470
471
472
# File 'lib/rbi/model.rb', line 470

def sigs
  @sigs ||= []
end

#visibilityObject

: Visibility



467
468
469
# File 'lib/rbi/model.rb', line 467

def visibility
  @visibility
end

Instance Method Details

#<<(param) ⇒ Object

: (Param param) -> void



511
512
513
# File 'lib/rbi/model.rb', line 511

def <<(param)
  params << param
end

#add_block_param(name) ⇒ Object

: (String name) -> void



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

def add_block_param(name)
  params << BlockParam.new(name)
end

#add_kw_opt_param(name, default_value) ⇒ Object

: (String name, String default_value) -> void



536
537
538
# File 'lib/rbi/model.rb', line 536

def add_kw_opt_param(name, default_value)
  params << KwOptParam.new(name, default_value)
end

#add_kw_param(name) ⇒ Object

: (String name) -> void



531
532
533
# File 'lib/rbi/model.rb', line 531

def add_kw_param(name)
  params << KwParam.new(name)
end

#add_kw_rest_param(name) ⇒ Object

: (String name) -> void



541
542
543
# File 'lib/rbi/model.rb', line 541

def add_kw_rest_param(name)
  params << KwRestParam.new(name)
end

#add_opt_param(name, default_value) ⇒ Object

: (String name, String default_value) -> void



521
522
523
# File 'lib/rbi/model.rb', line 521

def add_opt_param(name, default_value)
  params << OptParam.new(name, default_value)
end

#add_param(name) ⇒ Object

: (String name) -> void



516
517
518
# File 'lib/rbi/model.rb', line 516

def add_param(name)
  params << ReqParam.new(name)
end

#add_rest_param(name) ⇒ Object

: (String name) -> void



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

def add_rest_param(name)
  params << RestParam.new(name)
end

#add_sig(params: [], return_type: "void", is_abstract: false, is_override: false, is_overridable: false, is_final: false, type_params: [], checked: nil, &block) ⇒ Object

: ( | ?params: Array?, | ?return_type: (String | Type), | ?is_abstract: bool, | ?is_override: bool, | ?is_overridable: bool, | ?is_final: bool, | ?type_params: Array?, | ?checked: Symbol?) ?{ (Sig node) -> void } -> void



559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
# File 'lib/rbi/model.rb', line 559

def add_sig(
  params: [],
  return_type: "void",
  is_abstract: false,
  is_override: false,
  is_overridable: false,
  is_final: false,
  type_params: [],
  checked: nil,
  &block
)
  sig = Sig.new(
    params: params,
    return_type: return_type,
    is_abstract: is_abstract,
    is_override: is_override,
    is_overridable: is_overridable,
    is_final: is_final,
    type_params: type_params,
    checked: checked,
    &block
  )
  sigs << sig
end

#compatible_with?(other) ⇒ Boolean

: (Node other) -> bool

Returns:

  • (Boolean)


466
467
468
469
470
471
472
# File 'lib/rbi/rewriters/merge_trees.rb', line 466

def compatible_with?(other)
  other.is_a?(Method) &&
    name == other.name &&
    params == other.params &&
    at_most_one_side_anonymous?(other) &&
    (sigs.empty? || other.sigs.empty? || sigs == other.sigs)
end

#fully_qualified_nameObject

: -> String



585
586
587
588
589
590
591
# File 'lib/rbi/model.rb', line 585

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

#index_idsObject

: -> Array



114
115
116
# File 'lib/rbi/index.rb', line 114

def index_ids
  [fully_qualified_name]
end

#merge_with(other) ⇒ Object

: (Node other) -> void



476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# File 'lib/rbi/rewriters/merge_trees.rb', line 476

def merge_with(other)
  return unless other.is_a?(Method)

  super

  # If self is the all-anonymous side (since compatible_with? ensures
  # at most one side is), or if self has no sigs but other does, adopt
  # other's non-anonymous param names and sigs.
  if params.all?(&:anonymous?) || (sigs.empty? && !other.sigs.empty?)
    @params = other.params.dup
    @sigs = other.sigs.dup unless other.sigs.empty?
    return
  end

  other.sigs.each do |sig|
    sigs << sig unless sigs.include?(sig)
  end
end

#paramsObject

: -> Array



459
460
461
# File 'lib/rbi/model.rb', line 459

def params
  @params ||= []
end

#sigs?Boolean

: -> bool

Returns:

  • (Boolean)


475
476
477
# File 'lib/rbi/model.rb', line 475

def sigs?
  !@sigs.nil? && !@sigs.empty?
end

#to_sObject

: -> String



595
596
597
# File 'lib/rbi/model.rb', line 595

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