Class: RBS::Environment::ModuleEntry
- Inherits:
-
Object
- Object
- RBS::Environment::ModuleEntry
- Defined in:
- sig/environment/module_entry.rbs,
lib/rbs/environment/module_entry.rb
Overview
Represents a class entry in the environment
entry = ModuleEntry.new(TypeName.parse("::Kernel"))
entry << [nil, declaration]
entry << [[nil, TypeName.parse("::Object")], declaration]
Instance Attribute Summary collapse
-
#context_decls ⇒ Array[context_decl]
readonly
Returns the value of attribute context_decls.
-
#name ⇒ TypeName
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #<<(context_decl) ⇒ self
- #each_decl {|arg0| ... } ⇒ Object
-
#empty? ⇒ Boolean
Returns true if the entry doesn't have any declaration.
-
#initialize(name) ⇒ ModuleEntry
constructor
A new instance of ModuleEntry.
-
#primary_decl ⇒ declaration
Returns the first declaration.
- #self_types ⇒ Array[AST::Declarations::Module::Self]
-
#type_params ⇒ Array[AST::TypeParam]
Returns the generics declaration.
-
#validate_type_params ⇒ void
Confirms if the type parameters in the declaration are compatible.
Constructor Details
#initialize(name) ⇒ ModuleEntry
Returns a new instance of ModuleEntry.
10 11 12 13 |
# File 'lib/rbs/environment/module_entry.rb', line 10 def initialize(name) @name = name @context_decls = [] end |
Instance Attribute Details
#context_decls ⇒ Array[context_decl] (readonly)
Returns the value of attribute context_decls.
8 9 10 |
# File 'lib/rbs/environment/module_entry.rb', line 8 def context_decls @context_decls end |
#name ⇒ TypeName (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/rbs/environment/module_entry.rb', line 6 def name @name end |
Instance Method Details
#<<(context_decl) ⇒ self
15 16 17 18 |
# File 'lib/rbs/environment/module_entry.rb', line 15 def <<(context_decl) context_decls << context_decl self end |
#each_decl ⇒ void #each_decl ⇒ Enumerator[declaration]
20 21 22 23 24 25 26 27 28 |
# File 'lib/rbs/environment/module_entry.rb', line 20 def each_decl(&block) if block context_decls.each do |_, decl| yield decl end else enum_for(__method__ || raise) end end |
#empty? ⇒ Boolean
Returns true if the entry doesn't have any declaration
29 30 31 |
# File 'sig/environment/module_entry.rbs', line 29 def empty? context_decls.empty? end |
#primary_decl ⇒ declaration
Returns the first declaration
This method helps using the class with ClassEntry objects.
35 36 37 |
# File 'sig/environment/module_entry.rbs', line 35 def primary_decl each_decl.first or raise end |
#self_types ⇒ Array[AST::Declarations::Module::Self]
43 44 45 46 47 |
# File 'lib/rbs/environment/module_entry.rb', line 43 def self_types each_decl.flat_map do |decl| decl.self_types end.uniq end |
#type_params ⇒ Array[AST::TypeParam]
Returns the generics declaration
39 40 41 42 |
# File 'sig/environment/module_entry.rbs', line 39 def type_params validate_type_params primary_decl.type_params end |
#validate_type_params ⇒ void
This method returns an undefined value.
Confirms if the type parameters in the declaration are compatible
- Raises
GenericParameterMismatchErrorif incompatible declaration is detected.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'sig/environment/module_entry.rbs', line 45 def validate_type_params unless context_decls.empty? first_decl, *rest_decls = each_decl.to_a first_decl or raise first_params = first_decl.type_params first_names = first_params.map(&:name) rest_decls.each do |other_decl| other_params = other_decl.type_params unless first_names.size == other_params.size && first_params == AST::TypeParam.rename(other_params, new_names: first_names) raise GenericParameterMismatchError.new(name: name, decl: other_decl) end end end end |