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/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, #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: [], is_singleton: false, visibility: Public.new, sigs: [], loc: nil, comments: [], &block) ⇒ Method

Returns a new instance of Method.



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

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.



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

def is_singleton
  @is_singleton
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#paramsObject (readonly)

Returns the value of attribute params.



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

def params
  @params
end

#sigsObject

Returns the value of attribute sigs.



501
502
503
# File 'lib/rbi/model.rb', line 501

def sigs
  @sigs
end

#visibilityObject

Returns the value of attribute visibility.



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

def visibility
  @visibility
end

Instance Method Details

#<<(param) ⇒ Object



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

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

#add_block_param(name) ⇒ Object



570
571
572
# File 'lib/rbi/model.rb', line 570

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

#add_kw_opt_param(name, default_value) ⇒ Object



560
561
562
# File 'lib/rbi/model.rb', line 560

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

#add_kw_param(name) ⇒ Object



555
556
557
# File 'lib/rbi/model.rb', line 555

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

#add_kw_rest_param(name) ⇒ Object



565
566
567
# File 'lib/rbi/model.rb', line 565

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

#add_opt_param(name, default_value) ⇒ Object



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

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

#add_param(name) ⇒ Object



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

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

#add_rest_param(name) ⇒ Object



550
551
552
# File 'lib/rbi/model.rb', line 550

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



587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
# File 'lib/rbi/model.rb', line 587

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

Returns:

  • (Boolean)


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

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



613
614
615
616
617
618
619
# File 'lib/rbi/model.rb', line 613

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

#merge_with(other) ⇒ Object



478
479
480
481
482
483
484
485
# File 'lib/rbi/rewriters/merge_trees.rb', line 478

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

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

#to_sObject



622
623
624
# File 'lib/rbi/model.rb', line 622

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