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.

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, interface, &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.

  • interface (Boolean)

    A boolean indicating whether this module is an interface.

  • block

    A block which the new instance yields itself to.



25
26
27
28
29
# File 'lib/parlour/rbi_generator/module_namespace.rb', line 25

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

Instance Attribute Details

#interfaceBoolean (readonly)

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

Returns:

  • (Boolean)


53
54
55
# File 'lib/parlour/rbi_generator/module_namespace.rb', line 53

def interface
  @interface
end

Instance Method Details

#describeString

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

Returns:

  • (String)


91
92
93
94
# File 'lib/parlour/rbi_generator/module_namespace.rb', line 91

def describe
  "Module #{name} - #{"interface, " if interface}#{children.length} " +
    "children, #{includes.length} includes, #{extends.length} extends"
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.



42
43
44
45
46
47
48
# File 'lib/parlour/rbi_generator/module_namespace.rb', line 42

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 += 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:



84
85
86
# File 'lib/parlour/rbi_generator/module_namespace.rb', line 84

def merge_into_self(others)
  super
end

#mergeable?(others) ⇒ Boolean

Given an array of Parlour::RbiGenerator::ModuleNamespace 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.



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

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

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