Class: Parlour::RbsGenerator::Namespace
- Inherits:
-
RbsObject
- Object
- TypedObject
- RbsObject
- Parlour::RbsGenerator::Namespace
- Extended by:
- T::Generic, T::Sig
- Includes:
- Mixin::Searchable
- Defined in:
- lib/parlour/rbs_generator/namespace.rb
Overview
A generic namespace. This shouldn’t be used, except as the type of #root.
Direct Known Subclasses
Constant Summary collapse
- Child =
type_member {{ fixed: RbsObject }}
Instance Attribute Summary collapse
-
#children ⇒ Array<RbsObject>
readonly
The child RbsObject instances inside this namespace.
Attributes inherited from RbsObject
Attributes inherited from TypedObject
#comments, #generated_by, #name
Instance Method Summary collapse
-
#add_comment_to_next_child(comment) ⇒ void
Adds one or more comments to the next child RBS object to be created.
- #aliases ⇒ Array<RbsGenerator::TypeAlias> (also: #type_aliases)
- #constants ⇒ Array<RbsGenerator::Constant>
-
#create_arbitrary(code:, &block) ⇒ RbsGenerator::Arbitrary
Creates a new arbitrary code section.
-
#create_attr_accessor(name, type:, &block) ⇒ RbsGenerator::Attribute
Creates a new read and write attribute (
attr_accessor
). -
#create_attr_reader(name, type:, &block) ⇒ RbsGenerator::Attribute
Creates a new read-only attribute (
attr_reader
). -
#create_attr_writer(name, type:, &block) ⇒ RbsGenerator::Attribute
Creates a new write-only attribute (
attr_writer
). -
#create_attribute(name, kind:, type:, &block) ⇒ RbsGenerator::Attribute
(also: #create_attr)
Creates a new attribute.
-
#create_class(name, superclass: nil, &block) ⇒ ClassNamespace
Creates a new class definition as a child of this namespace.
-
#create_constant(name, type:, &block) ⇒ RbsGenerator::Constant
Adds a new constant definition to this namespace.
-
#create_extend(type, &block) ⇒ RbsGenerator::Extend
Adds a new
extend
to this namespace. -
#create_extends(extendables) ⇒ Array<RbsGenerator::Extend>
Adds new extends to this namespace.
-
#create_include(type, &block) ⇒ RbsGenerator::Include
Adds a new
include
to this namespace. -
#create_includes(includables) ⇒ Array<RbsGenerator::Include>
Adds new includes to this namespace.
-
#create_interface(name, &block) ⇒ InterfaceNamespace
Creates a new interface definition as a child of this namespace.
-
#create_method(name, signatures = nil, class_method: false, &block) ⇒ Method
Creates a new method definition as a child of this namespace.
-
#create_module(name, &block) ⇒ ModuleNamespace
Creates a new module definition as a child of this namespace.
-
#create_type_alias(name, type:, &block) ⇒ RbsGenerator::TypeAlias
Adds a new type alias, in the form of a constant, to this namespace.
- #describe_attrs ⇒ Object
- #extends ⇒ Array<RbsGenerator::Extend>
-
#generate_rbs(indent_level, options) ⇒ Array<String>
Generates the RBS lines for this namespace.
- #includes ⇒ Array<RbsGenerator::Include>
-
#initialize(generator, name = nil, &block) ⇒ void
constructor
Creates a new namespace.
-
#merge_into_self(others) ⇒ void
Given an array of Namespace instances, merges them into this one.
-
#mergeable?(others) ⇒ true
Given an array of Namespace instances, returns true if they may be merged into this instance using #merge_into_self.
-
#path(object, &block) ⇒ Object
Given a Class or Module object, generates all classes and modules in the path to that object, then executes the given block on the last Namespace.
Methods included from Mixin::Searchable
Methods inherited from TypedObject
#add_comment, #describe, #describe_tree
Constructor Details
#initialize(generator, name = nil, &block) ⇒ void
Unless you’re doing something impressively hacky, this shouldn’t be invoked outside of Parlour::RbsGenerator#initialize.
Creates a new namespace.
42 43 44 45 46 47 |
# File 'lib/parlour/rbs_generator/namespace.rb', line 42 def initialize(generator, name = nil, &block) super(generator, name || '<anonymous namespace>') @children = [] @next_comments = [] yield_self(&block) if block end |
Instance Attribute Details
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 RBS object to be created.
139 140 141 142 143 144 145 |
# File 'lib/parlour/rbs_generator/namespace.rb', line 139 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 |
#aliases ⇒ Array<RbsGenerator::TypeAlias> Also known as: type_aliases
80 81 82 83 84 85 |
# File 'lib/parlour/rbs_generator/namespace.rb', line 80 def aliases T.cast( children.select { |c| c.is_a?(RbsGenerator::TypeAlias) }, T::Array[RbsGenerator::TypeAlias] ) end |
#constants ⇒ Array<RbsGenerator::Constant>
91 92 93 94 95 96 |
# File 'lib/parlour/rbs_generator/namespace.rb', line 91 def constants T.cast( children.select { |c| c.is_a?(RbsGenerator::Constant) }, T::Array[RbsGenerator::Constant] ) end |
#create_arbitrary(code:, &block) ⇒ RbsGenerator::Arbitrary
Creates a new arbitrary code section. You should rarely have to use this!
357 358 359 360 361 362 363 364 365 366 |
# File 'lib/parlour/rbs_generator/namespace.rb', line 357 def create_arbitrary(code:, &block) new_arbitrary = RbsGenerator::Arbitrary.new( generator, code: code, &block ) move_next_comments(new_arbitrary) children << new_arbitrary new_arbitrary end |
#create_attr_accessor(name, type:, &block) ⇒ RbsGenerator::Attribute
Creates a new read and write attribute (attr_accessor
).
347 348 349 |
# File 'lib/parlour/rbs_generator/namespace.rb', line 347 def create_attr_accessor(name, type:, &block) create_attribute(name, kind: :accessor, type: type, &block) end |
#create_attr_reader(name, type:, &block) ⇒ RbsGenerator::Attribute
Creates a new read-only attribute (attr_reader
).
309 310 311 |
# File 'lib/parlour/rbs_generator/namespace.rb', line 309 def create_attr_reader(name, type:, &block) create_attribute(name, kind: :reader, type: type, &block) end |
#create_attr_writer(name, type:, &block) ⇒ RbsGenerator::Attribute
Creates a new write-only attribute (attr_writer
).
328 329 330 |
# File 'lib/parlour/rbs_generator/namespace.rb', line 328 def create_attr_writer(name, type:, &block) create_attribute(name, kind: :writer, type: type, &block) end |
#create_attribute(name, kind:, type:, &block) ⇒ RbsGenerator::Attribute Also known as: create_attr
Creates a new attribute.
280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/parlour/rbs_generator/namespace.rb', line 280 def create_attribute(name, kind:, type:, &block) new_attribute = RbsGenerator::Attribute.new( generator, name, kind, type, &block ) move_next_comments(new_attribute) children << new_attribute new_attribute end |
#create_class(name, superclass: nil, &block) ⇒ ClassNamespace
Creates a new class definition as a child of this namespace.
169 170 171 172 173 174 |
# File 'lib/parlour/rbs_generator/namespace.rb', line 169 def create_class(name, superclass: nil, &block) new_class = ClassNamespace.new(generator, name, superclass, &block) move_next_comments(new_class) children << new_class new_class end |
#create_constant(name, type:, &block) ⇒ RbsGenerator::Constant
Adds a new constant definition to this namespace.
450 451 452 453 454 455 456 457 458 459 460 |
# File 'lib/parlour/rbs_generator/namespace.rb', line 450 def create_constant(name, type:, &block) new_constant = RbsGenerator::Constant.new( generator, name, type: type, &block ) move_next_comments(new_constant) children << new_constant new_constant end |
#create_extend(type, &block) ⇒ RbsGenerator::Extend
Adds a new extend
to this namespace.
377 378 379 380 381 382 383 384 385 386 |
# File 'lib/parlour/rbs_generator/namespace.rb', line 377 def create_extend(type, &block) new_extend = RbsGenerator::Extend.new( generator, type: type, &block ) move_next_comments(new_extend) children << new_extend new_extend end |
#create_extends(extendables) ⇒ Array<RbsGenerator::Extend>
Adds new extends to this namespace.
396 397 398 399 400 401 402 |
# File 'lib/parlour/rbs_generator/namespace.rb', line 396 def create_extends(extendables) returned_extendables = [] extendables.each do |extendable| returned_extendables << create_extend(extendable) end returned_extendables end |
#create_include(type, &block) ⇒ RbsGenerator::Include
Adds a new include
to this namespace.
413 414 415 416 417 418 419 420 421 422 |
# File 'lib/parlour/rbs_generator/namespace.rb', line 413 def create_include(type, &block) new_include = RbsGenerator::Include.new( generator, type: type, &block ) move_next_comments(new_include) children << new_include new_include end |
#create_includes(includables) ⇒ Array<RbsGenerator::Include>
Adds new includes to this namespace.
432 433 434 435 436 437 438 |
# File 'lib/parlour/rbs_generator/namespace.rb', line 432 def create_includes(includables) returned_includables = [] includables.each do |includable| returned_includables << create_include(includable) end returned_includables end |
#create_interface(name, &block) ⇒ InterfaceNamespace
Creates a new interface definition as a child of this namespace.
211 212 213 214 215 216 |
# File 'lib/parlour/rbs_generator/namespace.rb', line 211 def create_interface(name, &block) new_interface = InterfaceNamespace.new(generator, name, &block) move_next_comments(new_interface) children << new_interface new_interface end |
#create_method(name, signatures = nil, class_method: false, &block) ⇒ Method
Creates a new method definition as a child of this namespace.
236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/parlour/rbs_generator/namespace.rb', line 236 def create_method(name, signatures = nil, class_method: false, &block) raise 'cannot have a method with no signatures' if signatures&.empty? new_method = RbsGenerator::Method.new( generator, name, signatures || [MethodSignature.new([], nil)], class_method: class_method, &block ) move_next_comments(new_method) children << new_method new_method end |
#create_module(name, &block) ⇒ ModuleNamespace
Creates a new module definition as a child of this namespace.
190 191 192 193 194 195 |
# File 'lib/parlour/rbs_generator/namespace.rb', line 190 def create_module(name, &block) new_module = ModuleNamespace.new(generator, name, &block) move_next_comments(new_module) children << new_module new_module end |
#create_type_alias(name, type:, &block) ⇒ RbsGenerator::TypeAlias
Adds a new type alias, in the form of a constant, to this namespace.
472 473 474 475 476 477 478 479 480 481 482 |
# File 'lib/parlour/rbs_generator/namespace.rb', line 472 def create_type_alias(name, type:, &block) new_type_alias = TypeAlias.new( generator, name: name, type: type, &block ) move_next_comments(new_type_alias) children << new_type_alias new_type_alias end |
#describe_attrs ⇒ Object
525 526 527 |
# File 'lib/parlour/rbs_generator/namespace.rb', line 525 def describe_attrs [:children] end |
#extends ⇒ Array<RbsGenerator::Extend>
60 61 62 63 64 65 |
# File 'lib/parlour/rbs_generator/namespace.rb', line 60 def extends T.cast( children.select { |c| c.is_a?(RbsGenerator::Extend) }, T::Array[RbsGenerator::Extend] ) end |
#generate_rbs(indent_level, options) ⇒ Array<String>
Generates the RBS lines for this namespace.
21 22 23 24 |
# File 'lib/parlour/rbs_generator/namespace.rb', line 21 def generate_rbs(indent_level, ) generate_comments(indent_level, ) + generate_body(indent_level, ) end |
#includes ⇒ Array<RbsGenerator::Include>
70 71 72 73 74 75 |
# File 'lib/parlour/rbs_generator/namespace.rb', line 70 def includes T.cast( children.select { |c| c.is_a?(RbsGenerator::Include) }, T::Array[RbsGenerator::Include] ) end |
#merge_into_self(others) ⇒ void
This method returns an undefined value.
Given an array of Parlour::RbsGenerator::Namespace instances, merges them into this one. All children, constants, extends and includes are copied into this instance.
There may also be Method instances in the stream, which are ignored.
515 516 517 518 519 520 521 522 |
# File 'lib/parlour/rbs_generator/namespace.rb', line 515 def merge_into_self(others) others.each do |other| next if other.is_a?(RbsGenerator::Method) other = T.cast(other, Namespace) other.children.each { |c| children << c } end end |
#mergeable?(others) ⇒ true
Given an array of Parlour::RbsGenerator::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.)
497 498 499 |
# File 'lib/parlour/rbs_generator/namespace.rb', line 497 def mergeable?(others) true end |
#path(object, &block) ⇒ Object
Given a Class or Module object, generates all classes and modules in the path to that object, then executes the given block on the last Parlour::RbsGenerator::Namespace. This should only be executed on the root namespace.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/parlour/rbs_generator/namespace.rb', line 104 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) elsif type == Module current_part = current_part.create_module(name) else raise "unexpected type: path part #{name} is a #{type}" end end block.call(current_part) end |