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, #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

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



410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'lib/rbi/model.rb', line 410

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

: bool



401
402
403
# File 'lib/rbi/model.rb', line 401

def is_singleton
  @is_singleton
end

#nameObject

: String



395
396
397
# File 'lib/rbi/model.rb', line 395

def name
  @name
end

#paramsObject (readonly)

: Array



398
399
400
# File 'lib/rbi/model.rb', line 398

def params
  @params
end

#sigsObject

: Array



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

def sigs
  @sigs
end

#visibilityObject

: Visibility



404
405
406
# File 'lib/rbi/model.rb', line 404

def visibility
  @visibility
end

Instance Method Details

#<<(param) ⇒ Object

: (Param param) -> void



430
431
432
# File 'lib/rbi/model.rb', line 430

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

#add_block_param(name) ⇒ Object

: (String name) -> void



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

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

#add_kw_opt_param(name, default_value) ⇒ Object

: (String name, String default_value) -> void



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

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

#add_kw_param(name) ⇒ Object

: (String name) -> void



450
451
452
# File 'lib/rbi/model.rb', line 450

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

#add_kw_rest_param(name) ⇒ Object

: (String name) -> void



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

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

#add_opt_param(name, default_value) ⇒ Object

: (String name, String default_value) -> void



440
441
442
# File 'lib/rbi/model.rb', line 440

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

#add_param(name) ⇒ Object

: (String name) -> void



435
436
437
# File 'lib/rbi/model.rb', line 435

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

#add_rest_param(name) ⇒ Object

: (String name) -> void



445
446
447
# File 'lib/rbi/model.rb', line 445

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



470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# File 'lib/rbi/model.rb', line 470

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)


440
441
442
443
444
445
446
# File 'lib/rbi/rewriters/merge_trees.rb', line 440

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

: -> String



496
497
498
499
500
501
502
# File 'lib/rbi/model.rb', line 496

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



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

def index_ids
  [fully_qualified_name]
end

#merge_with(other) ⇒ Object

: (Node other) -> void



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

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

: -> String



506
507
508
# File 'lib/rbi/model.rb', line 506

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