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 Node

#detach, #group_kind, #parent_conflict_tree, #parent_scope, #print, #replace, #string

Constructor Details

#initialize(name, names, visibility: Public.new, sigs: [], loc: nil, comments: []) ⇒ Attr

Returns a new instance of Attr.



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

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.



335
336
337
# File 'lib/rbi/model.rb', line 335

def names
  @names
end

#sigsObject (readonly)

Returns the value of attribute sigs.



341
342
343
# File 'lib/rbi/model.rb', line 341

def sigs
  @sigs
end

#visibilityObject

Returns the value of attribute visibility.



338
339
340
# File 'lib/rbi/model.rb', line 338

def visibility
  @visibility
end

Instance Method Details

#accept_printer(v) ⇒ Object



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/rbi/printer.rb', line 303

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

  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)


382
383
384
385
386
# File 'lib/rbi/rewriters/merge_trees.rb', line 382

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



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

def fully_qualified_names; end

#index_idsObject



102
103
104
# File 'lib/rbi/index.rb', line 102

def index_ids
  fully_qualified_names
end

#merge_with(other) ⇒ Object



389
390
391
392
393
394
395
# File 'lib/rbi/rewriters/merge_trees.rb', line 389

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)


331
332
333
# File 'lib/rbi/printer.rb', line 331

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