Class: Parlour::RbiGenerator::Namespace

Inherits:
RbiObject
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/parlour/rbi_generator/namespace.rb

Overview

A generic namespace. This shouldn’t be used, except as the type of #root.

Direct Known Subclasses

ClassNamespace, ModuleNamespace

Instance Attribute Summary collapse

Attributes inherited from RbiObject

#comments, #generated_by, #generator, #name

Instance Method Summary collapse

Methods inherited from RbiObject

#add_comment

Constructor Details

#initialize(generator, name = nil, &block) ⇒ void

Note:

Unless you’re doing something impressively hacky, this shouldn’t be invoked outside of Parlour::RbiGenerator#initialize.

Creates a new namespace.

Parameters:

  • generator (RbiGenerator)

    The current RbiGenerator.

  • name (String, nil) (defaults to: nil)

    The name of this module.

  • block

    A block which the new instance yields itself to.



40
41
42
43
44
45
# File 'lib/parlour/rbi_generator/namespace.rb', line 40

def initialize(generator, name = nil, &block)
  super(generator, name || '<anonymous namespace>')
  @children = []
  @next_comments = []
  yield_self(&block)
end

Instance Attribute Details

#childrenArray<RbiObject> (readonly)

The child RbiObject instances inside this namespace.

Returns:



50
51
52
# File 'lib/parlour/rbi_generator/namespace.rb', line 50

def children
  @children
end

Instance Method Details

#add_comment_to_next_child(comment) ⇒ void

This method returns an undefined value.

Adds one or more comments to the next child RBI object to be created.

Examples:

Creating a module with a comment.

namespace.add_comment_to_next_child('This is a module')
namespace.create_module(name: 'M')

Creating a class with a multi-line comment.

namespace.add_comment_to_next_child(['This is a multi-line comment!', 'It can be as long as you want!'])
namespace.create_class(name: 'C')

Parameters:

  • comment (String, Array<String>)

    The new comment(s).



123
124
125
126
127
128
129
# File 'lib/parlour/rbi_generator/namespace.rb', line 123

def add_comment_to_next_child(comment)
  if comment.is_a?(String)
    @next_comments << comment
  elsif comment.is_a?(Array)
    @next_comments.concat(comment)
  end
end

#constantsArray<RbiGenerator::Constant>

The Constant objects from #children.

Returns:



75
76
77
78
79
80
# File 'lib/parlour/rbi_generator/namespace.rb', line 75

def constants
  T.cast(
    children.select { |c| c.is_a?(RbiGenerator::Constant) },
    T::Array[RbiGenerator::Constant]
  )
end

#create_arbitrary(code: nil, &block) ⇒ RbiGenerator::Arbitrary

Creates a new arbitrary code section. You should rarely have to use this!

Parameters:

  • code (String) (defaults to: nil)

    The code to insert.

  • block

    A block which the new instance yields itself to.

Returns:



328
329
330
331
332
333
334
335
336
337
338
# File 'lib/parlour/rbi_generator/namespace.rb', line 328

def create_arbitrary(code: nil, &block)
  code = T.must(code)
  new_arbitrary = RbiGenerator::Arbitrary.new(
    generator,
    code: code,
    &block
  )
  move_next_comments(new_arbitrary)
  children << new_arbitrary
  new_arbitrary
end

#create_attr_accessor(name: nil, type: nil, &block) ⇒ RbiGenerator::Attribute

Creates a new read and write attribute (attr_accessor).

Parameters:

  • name (String) (defaults to: nil)

    The name of this attribute.

  • type (String) (defaults to: nil)

    A Sorbet string of this attribute’s type, such as “String” or “T.untyped”.

  • block

    A block which the new instance yields itself to.

Returns:



318
319
320
# File 'lib/parlour/rbi_generator/namespace.rb', line 318

def create_attr_accessor(name: nil, type: nil, &block)
  create_attribute(name: name, kind: :accessor, type: type, &block)
end

#create_attr_reader(name: nil, type: nil, &block) ⇒ RbiGenerator::Attribute

Creates a new read-only attribute (attr_reader).

Parameters:

  • name (String) (defaults to: nil)

    The name of this attribute.

  • type (String) (defaults to: nil)

    A Sorbet string of this attribute’s type, such as “String” or “T.untyped”.

  • block

    A block which the new instance yields itself to.

Returns:



296
297
298
# File 'lib/parlour/rbi_generator/namespace.rb', line 296

def create_attr_reader(name: nil, type: nil, &block)
  create_attribute(name: name, kind: :reader, type: type, &block)
end

#create_attr_writer(name: nil, type: nil, &block) ⇒ RbiGenerator::Attribute

Creates a new write-only attribute (attr_writer).

Parameters:

  • name (String) (defaults to: nil)

    The name of this attribute.

  • type (String) (defaults to: nil)

    A Sorbet string of this attribute’s type, such as “String” or “T.untyped”.

  • block

    A block which the new instance yields itself to.

Returns:



307
308
309
# File 'lib/parlour/rbi_generator/namespace.rb', line 307

def create_attr_writer(name: nil, type: nil, &block)
  create_attribute(name: name, kind: :writer, type: type, &block)
end

#create_attribute(name: nil, kind: nil, type: nil, &block) ⇒ RbiGenerator::Attribute Also known as: create_attr

Creates a new attribute.

Examples:

Create an attr_reader.

module.create_attribute('readable', :reader, 'String')
# #=> sig { returns(String) }
#     attr_reader :readable

Create an attr_writer.

module.create_attribute('writable', :writer, 'Integer')
# #=> sig { params(writable: Integer).returns(Integer) }
#     attr_writer :writable

Create an attr_accessor.

module.create_attribute('accessible', :accessor, 'T::Boolean')
# #=> sig { returns(T::Boolean) }
#     attr_accessor :accessible

Parameters:

  • name (String) (defaults to: nil)

    The name of this attribute.

  • kind (Symbol) (defaults to: nil)

    The kind of attribute this is; one of :writer, :reader, or :accessor.

  • type (String) (defaults to: nil)

    A Sorbet string of this attribute’s type, such as “String” or “T.untyped”.

  • block

    A block which the new instance yields itself to.

Returns:



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/parlour/rbi_generator/namespace.rb', line 272

def create_attribute(name: nil, kind: nil, type: nil, &block)
  name = T.must(name)
  kind = T.must(kind)
  type = T.must(type)
  new_attribute = RbiGenerator::Attribute.new(
    generator,
    name,
    kind,
    type,
    &block
  )
  move_next_comments(new_attribute)
  children << new_attribute
  new_attribute
end

#create_class(name: nil, superclass: nil, abstract: false, &block) ⇒ ClassNamespace

Creates a new class definition as a child of this namespace.

Examples:

Create a class with a nested module.

namespace.create_class(name: 'Foo') do |foo|
  foo.create_module(name: 'Bar')
end

Create a class that is the child of another class.

namespace.create_class(name: 'Bar', superclass: 'Foo') #=> class Bar < Foo

Parameters:

  • name (String) (defaults to: nil)

    The name of this class.

  • superclass (String, nil) (defaults to: nil)

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

  • abstract (Boolean) (defaults to: false)

    A boolean indicating whether this class is abstract.

  • block

    A block which the new instance yields itself to.

Returns:



155
156
157
158
159
160
161
# File 'lib/parlour/rbi_generator/namespace.rb', line 155

def create_class(name: nil, superclass: nil, abstract: false, &block)
  name = T.must(name)
  new_class = ClassNamespace.new(generator, name, superclass, abstract, &block)
  move_next_comments(new_class)
  children << new_class
  new_class
end

#create_constant(name: nil, value: nil, &block) ⇒ RbiGenerator::Constant

Adds a new constant definition to this namespace.

Examples:

Add an Elem constant to the class.

class.create_include(name: 'IncludableClass') #=> Elem = String

Parameters:

  • name (String) (defaults to: nil)

    The name of the constant.

  • value (String) (defaults to: nil)

    The value of the constant, as a Ruby code string.

  • block

    A block which the new instance yields itself to.

Returns:



394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/parlour/rbi_generator/namespace.rb', line 394

def create_constant(name: nil, value: nil, &block)
  name = T.must(name)
  value = T.must(value)
  new_constant = RbiGenerator::Constant.new(
    generator,
    name: name,
    value: value,
    &block
  )
  move_next_comments(new_constant)
  children << new_constant
  new_constant
end

#create_extend(name: nil, &block) ⇒ RbiGenerator::Extend

Adds a new extend to this namespace.

Examples:

Add an extend to a class.

class.create_extend(name: 'ExtendableClass') #=> extend ExtendableClass

Parameters:

  • object (String)

    A code string for what is extended, for example “MyModule”.

  • block

    A block which the new instance yields itself to.

Returns:



350
351
352
353
354
355
356
357
358
359
360
# File 'lib/parlour/rbi_generator/namespace.rb', line 350

def create_extend(name: nil, &block)
  name = T.must(name)
  new_extend = RbiGenerator::Extend.new(
    generator,
    name: name,
    &block
  )
  move_next_comments(new_extend)
  children << new_extend
  new_extend
end

#create_include(name: nil, &block) ⇒ RbiGenerator::Include

Adds a new include to this namespace.

Examples:

Add an include to a class.

class.create_include(name:  'IncludableClass') #=> include IncludableClass

Parameters:

  • object (String)

    A code string for what is included, for example “Enumerable”.

  • block

    A block which the new instance yields itself to.

Returns:



372
373
374
375
376
377
378
379
380
381
382
# File 'lib/parlour/rbi_generator/namespace.rb', line 372

def create_include(name: nil, &block)
  name = T.must(name)
  new_include = RbiGenerator::Include.new(
    generator,
    name: name,
    &block
  )
  move_next_comments(new_include)
  children << new_include
  new_include
end

#create_method(name: nil, parameters: nil, return_type: nil, returns: nil, abstract: false, implementation: false, override: false, overridable: false, class_method: false, &block) ⇒ Method

Creates a new method definition as a child of this namespace.

Parameters:

  • name (String) (defaults to: nil)

    The name of this method. You should not specify self. in this - use the class_method parameter instead.

  • parameters (Array<Parameter>) (defaults to: nil)

    An array of Parameter instances representing this method’s parameters.

  • return_type (String, nil) (defaults to: nil)

    A Sorbet string of what this method returns, such as “String” or “T.untyped”. Passing nil denotes a void return.

  • returns (String, nil) (defaults to: nil)

    Same as return_type.

  • abstract (Boolean) (defaults to: false)

    Whether this method is abstract.

  • implementation (Boolean) (defaults to: false)

    Whether this method is an implementation of a parent abstract method.

  • override (Boolean) (defaults to: false)

    Whether this method is overriding a parent overridable method.

  • overridable (Boolean) (defaults to: false)

    Whether this method is overridable by subclasses.

  • class_method (Boolean) (defaults to: false)

    Whether this method is a class method; that is, it it is defined using self..

  • block

    A block which the new instance yields itself to.

Returns:



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/parlour/rbi_generator/namespace.rb', line 226

def create_method(name: nil, parameters: nil, return_type: nil, returns: nil, abstract: false, implementation: false, override: false, overridable: false, class_method: false, &block)
  name = T.must(name)
  parameters = parameters || []
  raise 'cannot specify both return_type: and returns:' if return_type && returns
  return_type ||= returns
  new_method = RbiGenerator::Method.new(
    generator,
    name,
    parameters,
    return_type,
    abstract: abstract,
    implementation: implementation, 
    override: override,
    overridable: overridable,
    class_method: class_method,
    &block
  )
  move_next_comments(new_method)
  children << new_method
  new_method
end

#create_module(name: nil, interface: false, &block) ⇒ ModuleNamespace

Creates a new module definition as a child of this namespace.

Examples:

Create a basic module.

namespace.create_module(name: 'Foo')

Create a module with a method.

namespace.create_module(name: 'Foo') do |foo|
  foo.create_method(name: 'method_name', parameters: [], return_type: 'Integer')
end

Parameters:

  • name (String) (defaults to: nil)

    The name of this module.

  • interface (Boolean) (defaults to: false)

    A boolean indicating whether this module is an interface.

  • block

    A block which the new instance yields itself to.

Returns:



185
186
187
188
189
190
191
# File 'lib/parlour/rbi_generator/namespace.rb', line 185

def create_module(name: nil, interface: false, &block)
  name = T.must(name)
  new_module = ModuleNamespace.new(generator, name, interface, &block)
  move_next_comments(new_module)
  children << new_module
  new_module
end

#describeString

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

Returns:

  • (String)


448
449
450
451
# File 'lib/parlour/rbi_generator/namespace.rb', line 448

def describe
  "Namespace #{name} - #{children.length} children, #{includes.length} " +
    "includes, #{extends.length} extends, #{constants.length} constants"
end

#extendsArray<RbiGenerator::Extend>

The Extend objects from #children.

Returns:



55
56
57
58
59
60
# File 'lib/parlour/rbi_generator/namespace.rb', line 55

def extends
  T.cast(
    children.select { |c| c.is_a?(RbiGenerator::Extend) },
    T::Array[RbiGenerator::Extend]
  )
end

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

Generates the RBI lines for this namespace.

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.



20
21
22
23
# File 'lib/parlour/rbi_generator/namespace.rb', line 20

def generate_rbi(indent_level, options)
  generate_comments(indent_level, options) +
    generate_body(indent_level, options)
end

#includesArray<RbiGenerator::Include>

The Include objects from #children.

Returns:



65
66
67
68
69
70
# File 'lib/parlour/rbi_generator/namespace.rb', line 65

def includes
  T.cast(
    children.select { |c| c.is_a?(RbiGenerator::Include) },
    T::Array[RbiGenerator::Include]
  )
end

#merge_into_self(others) ⇒ void

This method returns an undefined value.

Given an array of Parlour::RbiGenerator::Namespace instances, merges them into this one. All children, constants, extends and includes are copied into this instance.

Parameters:



436
437
438
439
440
441
442
# File 'lib/parlour/rbi_generator/namespace.rb', line 436

def merge_into_self(others)
  others.each do |other|
    other = T.cast(other, Namespace)

    other.children.each { |c| children << c }
  end
end

#mergeable?(others) ⇒ true

Given an array of Parlour::RbiGenerator::Namespace instances, returns true if they may be merged into this instance using #merge_into_self. All bare namespaces can be merged into each other, as they lack definitions for themselves, so there is nothing to conflict. (This isn’t the case for subclasses such as ClassNamespace.)

Parameters:

Returns:

  • (true)

    Always true.



421
422
423
# File 'lib/parlour/rbi_generator/namespace.rb', line 421

def mergeable?(others)
  true
end

#path(object, &block) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/parlour/rbi_generator/namespace.rb', line 88

def path(object, &block)
  raise 'only call #path on root' if is_a?(ClassNamespace) || is_a?(ModuleNamespace)

  parts = object.to_s.split('::')
  parts_with_types = parts.size.times.map do |i|
    [parts[i], Module.const_get(parts[0..i].join('::')).class]
  end

  current_part = self
  parts_with_types.each do |(name, type)|
    if type == Class
      current_part = current_part.create_class(name: name)
    elsif type == Module
      current_part = current_part.create_module(name: name)
    else
      raise "unexpected type: path part #{name} is a #{type}"
    end
  end

  block.call(current_part)
end