Class: RBI::Attr

Inherits:
NodeWithComments show all
Extended by:
T::Helpers, 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

Attributes

Direct Known Subclasses

AttrAccessor, AttrReader, AttrWriter

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, names, visibility: Public.new, sigs: [], loc: nil, comments: []) ⇒ Attr

Returns a new instance of Attr.



366
367
368
369
370
371
# File 'lib/rbi/model.rb', line 366

def initialize(name, names, visibility: Public.new, sigs: [], loc: nil, comments: [])
  super(loc: loc, comments: comments)
  @names = T.let([name, *names], T::Array[Symbol])
  @visibility = visibility
  @sigs = sigs
end

Instance Attribute Details

#namesObject

Returns the value of attribute names.



348
349
350
# File 'lib/rbi/model.rb', line 348

def names
  @names
end

#sigsObject (readonly)

Returns the value of attribute sigs.



354
355
356
# File 'lib/rbi/model.rb', line 354

def sigs
  @sigs
end

#visibilityObject

Returns the value of attribute visibility.



351
352
353
# File 'lib/rbi/model.rb', line 351

def visibility
  @visibility
end

Instance Method Details

#accept_printer(v) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/rbi/printer.rb', line 318

def accept_printer(v)
  print_blank_line_before(v)

  v.visit_all(comments)
  sigs.each { |sig| v.visit(sig) }
  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
  case self
  when AttrAccessor
    v.print("attr_accessor")
  when AttrReader
    v.print("attr_reader")
  when AttrWriter
    v.print("attr_writer")
  end
  unless names.empty?
    v.print(" ")
    v.print(names.map { |name| ":#{name}" }.join(", "))
  end
  v.printn
end

#compatible_with?(other) ⇒ Boolean

Returns:

  • (Boolean)


406
407
408
409
410
# File 'lib/rbi/rewriters/merge_trees.rb', line 406

def compatible_with?(other)
  return false unless other.is_a?(Attr)
  return false unless names == other.names
  sigs.empty? || other.sigs.empty? || sigs == other.sigs
end

#fully_qualified_namesObject



374
# File 'lib/rbi/model.rb', line 374

def fully_qualified_names; end

#index_idsObject



109
110
111
# File 'lib/rbi/index.rb', line 109

def index_ids
  fully_qualified_names
end

#merge_with(other) ⇒ Object



413
414
415
416
417
418
419
# File 'lib/rbi/rewriters/merge_trees.rb', line 413

def merge_with(other)
  return unless other.is_a?(Attr)
  super
  other.sigs.each do |sig|
    sigs << sig unless sigs.include?(sig)
  end
end

#oneline?Boolean

Returns:

  • (Boolean)


345
346
347
# File 'lib/rbi/printer.rb', line 345

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