Class: Parlour::RbiGenerator::ClassNamespace

Inherits:
Namespace show all
Extended by:
T::Sig
Defined in:
lib/parlour/rbi_generator/class_namespace.rb

Overview

Represents a class definition.

Instance Attribute Summary collapse

Attributes inherited from Namespace

#children

Attributes inherited from RbiObject

#comments, #generated_by, #generator, #name

Instance Method Summary collapse

Methods inherited from Namespace

#add_comment_to_next_child, #constants, #create_arbitrary, #create_attr_accessor, #create_attr_reader, #create_attr_writer, #create_attribute, #create_class, #create_constant, #create_extend, #create_extends, #create_include, #create_includes, #create_method, #create_module, #extends, #includes, #path

Methods inherited from RbiObject

#add_comment

Constructor Details

#initialize(generator, name, superclass, abstract, &block) ⇒ void

Note:

You should use Namespace#create_class rather than this directly.

Creates a new class definition.

Parameters:

  • generator (RbiGenerator)

    The current RbiGenerator.

  • name (String)

    The name of this class.

  • superclass (String, nil)

    The superclass of this class, or nil if it doesn’t have one.

  • abstract (Boolean)

    A boolean indicating whether this class is abstract.

  • block

    A block which the new instance yields itself to.



27
28
29
30
31
# File 'lib/parlour/rbi_generator/class_namespace.rb', line 27

def initialize(generator, name, superclass, abstract, &block)
  super(generator, name, &block)
  @superclass = superclass
  @abstract = abstract
end

Instance Attribute Details

#abstractBoolean (readonly)

A boolean indicating whether this class is abstract or not.

Returns:

  • (Boolean)


64
65
66
# File 'lib/parlour/rbi_generator/class_namespace.rb', line 64

def abstract
  @abstract
end

#superclassString? (readonly)

The superclass of this class, or nil if it doesn’t have one.

Returns:

  • (String, nil)


59
60
61
# File 'lib/parlour/rbi_generator/class_namespace.rb', line 59

def superclass
  @superclass
end

Instance Method Details

#describeString

Returns a human-readable brief string description of this class.

Returns:

  • (String)


109
110
111
112
113
# File 'lib/parlour/rbi_generator/class_namespace.rb', line 109

def describe
  "Class #{name} - #{"superclass #{superclass}, " if superclass}" +
    "#{"abstract, " if abstract}#{children.length} children, " +
    "#{includes.length} includes, #{extends.length} extends"
end

#generate_rbi(indent_level, options) ⇒ Array<String>

Generates the RBI lines for this class.

Parameters:

  • indent_level (Integer)

    The indentation level to generate the lines at.

  • options (Options)

    The formatting options to use.

Returns:

  • (Array<String>)

    The RBI lines, formatted as specified.



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/parlour/rbi_generator/class_namespace.rb', line 44

def generate_rbi(indent_level, options)
  class_definition = superclass.nil? \
    ? "class #{name}"
    : "class #{name} < #{superclass}"
  
  lines = generate_comments(indent_level, options)
  lines << options.indented(indent_level, class_definition)
  lines += [options.indented(indent_level + 1, "abstract!"), ""] if abstract
  lines += generate_body(indent_level + 1, options)
  lines << options.indented(indent_level, "end")
end

#merge_into_self(others) ⇒ void

This method returns an undefined value.

Given an array of Parlour::RbiGenerator::ClassNamespace instances, merges them into this one. You MUST ensure that #mergeable? is true for those instances.

Parameters:



96
97
98
99
100
101
102
103
104
# File 'lib/parlour/rbi_generator/class_namespace.rb', line 96

def merge_into_self(others)
  super

  others.each do |other|
    other = T.cast(other, ClassNamespace)

    @superclass = other.superclass unless superclass
  end
end

#mergeable?(others) ⇒ Boolean

Given an array of Parlour::RbiGenerator::ClassNamespace instances, returns true if they may be merged into this instance using #merge_into_self. For instances to be mergeable, they must either all be abstract or all not be abstract, and they must define the same superclass (or none at all).

Parameters:

Returns:

  • (Boolean)

    Whether this instance may be merged with them.



78
79
80
81
82
83
84
# File 'lib/parlour/rbi_generator/class_namespace.rb', line 78

def mergeable?(others)
  others = T.cast(others, T::Array[ClassNamespace]) rescue (return false)
  all = others + [self]

  all.map(&:abstract).uniq.length == 1 &&
    all.map(&:superclass).compact.uniq.length <= 1
end