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 Node

#detach, #group_kind, #parent_conflict_tree, #parent_scope, #print, #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.



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

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.



472
473
474
# File 'lib/rbi/model.rb', line 472

def is_singleton
  @is_singleton
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#paramsObject (readonly)

Returns the value of attribute params.



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

def params
  @params
end

#sigsObject

Returns the value of attribute sigs.



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

def sigs
  @sigs
end

#visibilityObject

Returns the value of attribute visibility.



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

def visibility
  @visibility
end

Instance Method Details

#<<(param) ⇒ Object



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

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

#accept_printer(v) ⇒ Object



340
341
342
343
344
345
346
347
348
349
350
351
352
353
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
# File 'lib/rbi/printer.rb', line 340

def accept_printer(v)
  previous_node = v.previous_node
  v.printn if previous_node && (!previous_node.oneline? || !oneline?)

  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.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.text.strip}")
        end
        v.printn
      end
      v.dedent
    end
    v.print(")")
  end
  v.print("; end")
  v.printn
end

#compatible_with?(other) ⇒ Boolean

Returns:

  • (Boolean)


429
430
431
432
433
434
# File 'lib/rbi/rewriters/merge_trees.rb', line 429

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



517
518
519
520
521
522
523
# File 'lib/rbi/model.rb', line 517

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

#index_idsObject



112
113
114
# File 'lib/rbi/index.rb', line 112

def index_ids
  [fully_qualified_name]
end

#inline_params?Boolean

Returns:

  • (Boolean)


393
394
395
# File 'lib/rbi/printer.rb', line 393

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

#merge_with(other) ⇒ Object



437
438
439
440
441
442
443
# File 'lib/rbi/rewriters/merge_trees.rb', line 437

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)


388
389
390
# File 'lib/rbi/printer.rb', line 388

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

#to_sObject



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

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