Class: RBS::Environment::ClassEntry
- Inherits:
-
Object
- Object
- RBS::Environment::ClassEntry
- Defined in:
- sig/environment/class_entry.rbs,
lib/rbs/environment/class_entry.rb
Overview
Represents a class entry in the environment
entry = ClassEntry.new(TypeName.parse("::String"))
entry << [nil, declaration]
entry << [[nil, TypeName.parse("::Kernel")], 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) ⇒ ClassEntry
constructor
A new instance of ClassEntry.
- #primary_decl ⇒ Object
-
#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) ⇒ ClassEntry
Returns a new instance of ClassEntry.
10 11 12 13 |
# File 'lib/rbs/environment/class_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/class_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/class_entry.rb', line 6 def name @name end |
Instance Method Details
#<<(context_decl) ⇒ self
15 16 17 18 19 |
# File 'lib/rbs/environment/class_entry.rb', line 15 def <<(context_decl) context_decls << context_decl @primary_decl = nil self end |
#each_decl ⇒ void #each_decl ⇒ Enumerator[declaration]
21 22 23 24 25 26 27 28 29 |
# File 'lib/rbs/environment/class_entry.rb', line 21 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
30 31 32 |
# File 'sig/environment/class_entry.rbs', line 30 def empty? context_decls.empty? end |
#primary_decl ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/rbs/environment/class_entry.rb', line 35 def primary_decl @primary_decl ||= nil.tap do # @type break: declaration decl = each_decl.find {|decl| decl.super_class } break decl if decl decl = each_decl.first break decl if decl end || raise("Cannot find primary declaration for #{name}") end |
#type_params ⇒ Array[AST::TypeParam]
Returns the generics declaration
41 42 43 44 |
# File 'sig/environment/class_entry.rbs', line 41 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.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'sig/environment/class_entry.rbs', line 47 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 |