Class: Parlour::RbiGenerator::ModuleNamespace

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

Overview

Represents a module definition.

Constant Summary collapse

Child =
type_member {{ fixed: RbiObject }}

Instance Attribute Summary collapse

Attributes inherited from Namespace

#children, #final, #sealed

Attributes inherited from RbiObject

#generator

Attributes inherited from TypedObject

#comments, #generated_by, #name

Instance Method Summary collapse

Methods inherited from Namespace

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

Methods included from Mixin::Searchable

#children, #find, #find_all

Methods inherited from TypedObject

#add_comment, #describe, #describe_tree

Constructor Details

#initialize(generator, name, final, sealed, interface, abstract, &block) ⇒ void

Note:

You should use Namespace#create_module rather than this directly.

Creates a new module definition.

Parameters:

  • generator (RbiGenerator)

    The current RbiGenerator.

  • name (String)

    The name of this module.

  • final (Boolean)

    Whether this namespace is final.

  • sealed (Boolean)

    Whether this namespace is sealed.

  • interface (Boolean)

    A boolean indicating whether this module is an interface.

  • abstract (Boolean)

    A boolean indicating whether this module is abstract.

  • block

    A block which the new instance yields itself to.



33
34
35
36
37
38
# File 'lib/parlour/rbi_generator/module_namespace.rb', line 33

def initialize(generator, name, final, sealed, interface, abstract, &block)
  super(generator, name, final, sealed, &T.cast(block, T.nilable(T.proc.params(x: Namespace).void)))
  @name = name
  @interface = interface
  @abstract = abstract
end

Instance Attribute Details

#abstractBoolean (readonly)

A boolean indicating whether this module is abstract or not.

Returns:

  • (Boolean)


68
69
70
# File 'lib/parlour/rbi_generator/module_namespace.rb', line 68

def abstract
  @abstract
end

#interfaceBoolean (readonly)

A boolean indicating whether this module is an interface or not.

Returns:

  • (Boolean)


63
64
65
# File 'lib/parlour/rbi_generator/module_namespace.rb', line 63

def interface
  @interface
end

Instance Method Details

#describe_attrsObject



111
112
113
# File 'lib/parlour/rbi_generator/module_namespace.rb', line 111

def describe_attrs
  [:children, :abstract, :interface, :final, :sealed]
end

#generalize_from_rbi!Object



106
107
108
# File 'lib/parlour/rbi_generator/module_namespace.rb', line 106

def generalize_from_rbi!
  super
end

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

Generates the RBI lines for this module.

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.



51
52
53
54
55
56
57
58
# File 'lib/parlour/rbi_generator/module_namespace.rb', line 51

def generate_rbi(indent_level, options)
  lines = generate_comments(indent_level, options)
  lines << options.indented(indent_level, "module #{name}")
  lines += [options.indented(indent_level + 1, "interface!"), ""] if interface
  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::ModuleNamespace instances, merges them into this one. You MUST ensure that #mergeable? is true for those instances.

Parameters:



101
102
103
# File 'lib/parlour/rbi_generator/module_namespace.rb', line 101

def merge_into_self(others)
  super
end

#mergeable?(others) ⇒ Boolean

Given an array of Namespace 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 interfaces or all not be interfaces.

Parameters:

Returns:

  • (Boolean)

    Whether this instance may be merged with them.



82
83
84
85
86
87
88
89
# File 'lib/parlour/rbi_generator/module_namespace.rb', line 82

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

  all_modules = T.cast(all.select { |x| ModuleNamespace === x }, T::Array[ModuleNamespace])

  all_modules.map(&:interface).uniq.length == 1
end