Class: RBS::Environment::ModuleEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/environment/module_entry.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_declsObject (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

#nameObject (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) ⇒ Object



15
16
17
18
# File 'lib/rbs/environment/module_entry.rb', line 15

def <<(context_decl)
  context_decls << context_decl
  self
end

#each_decl(&block) ⇒ Object



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:

  • (Boolean)


30
31
32
# File 'lib/rbs/environment/module_entry.rb', line 30

def empty?
  context_decls.empty?
end

#primary_declObject



34
35
36
# File 'lib/rbs/environment/module_entry.rb', line 34

def primary_decl
  each_decl.first or raise
end

#self_typesObject



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_paramsObject



38
39
40
41
# File 'lib/rbs/environment/module_entry.rb', line 38

def type_params
  validate_type_params
  primary_decl.type_params
end

#validate_type_paramsObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rbs/environment/module_entry.rb', line 49

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