Class: RBI::Struct

Inherits:
Scope show all
Extended by:
T::Sig
Defined in:
lib/rbi/model.rb,
lib/rbi/printer.rb,
lib/rbi/rewriters/merge_trees.rb

Instance Attribute Summary collapse

Attributes inherited from Tree

#nodes

Attributes inherited from NodeWithComments

#comments

Attributes inherited from Node

#loc, #parent_tree

Instance Method Summary collapse

Methods inherited from Scope

#accept_printer, #dup_empty, #index_ids, #print_body, #to_s

Methods included from Indexable

#index_ids

Methods inherited from Tree

#<<, #accept_printer, #add_sig_templates!, #annotate!, #deannotate!, #empty?, #group_nodes!, #index, #merge, #nest_non_public_methods!, #nest_singleton_methods!, #oneline?, #sort_nodes!

Methods inherited from NodeWithComments

#annotations, #merge_with, #oneline?

Methods inherited from Node

#accept_printer, #detach, #group_kind, #merge_with, #oneline?, #parent_conflict_tree, #parent_scope, #print, #print_blank_line_before, #replace, #string

Constructor Details

#initialize(name, members: [], keyword_init: false, loc: nil, comments: [], &block) ⇒ Struct

Returns a new instance of Struct.



288
289
290
291
292
293
294
# File 'lib/rbi/model.rb', line 288

def initialize(name, members: [], keyword_init: false, loc: nil, comments: [], &block)
  super(loc: loc, comments: comments) {}
  @name = name
  @members = members
  @keyword_init = keyword_init
  block&.call(self)
end

Instance Attribute Details

#keyword_initObject

Returns the value of attribute keyword_init.



276
277
278
# File 'lib/rbi/model.rb', line 276

def keyword_init
  @keyword_init
end

#membersObject

Returns the value of attribute members.



273
274
275
# File 'lib/rbi/model.rb', line 273

def members
  @members
end

#nameObject

Returns the value of attribute name.



270
271
272
# File 'lib/rbi/model.rb', line 270

def name
  @name
end

Instance Method Details

#compatible_with?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

def compatible_with?(other)
  other.is_a?(Struct) && members == other.members && keyword_init == other.keyword_init
end

#fully_qualified_nameObject



297
298
299
300
# File 'lib/rbi/model.rb', line 297

def fully_qualified_name
  return name if name.start_with?("::")
  "#{parent_scope&.fully_qualified_name}::#{name}"
end


270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/rbi/printer.rb', line 270

def print_header(v)
  v.printt("#{name} = ::Struct.new")
  if !members.empty? || keyword_init
    v.print("(")
    args = members.map { |member| ":#{member}" }
    args << "keyword_init: true" if keyword_init
    v.print(args.join(", "))
    v.print(")")
  end
  if empty?
    v.printn
  else
    v.printn(" do")
  end
end