Class: Gelauto::TypeSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/gelauto/type_set.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTypeSet

Returns a new instance of TypeSet.



9
10
11
# File 'lib/gelauto/type_set.rb', line 9

def initialize
  @set = {}
end

Instance Attribute Details

#setObject (readonly)

Returns the value of attribute set.



7
8
9
# File 'lib/gelauto/type_set.rb', line 7

def set
  @set
end

Instance Method Details

#<<(type) ⇒ Object



21
22
23
# File 'lib/gelauto/type_set.rb', line 21

def <<(type)
  set[type.ruby_type] = type
end

#each(&block) ⇒ Object



35
36
37
# File 'lib/gelauto/type_set.rb', line 35

def each(&block)
  set.each_value(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/gelauto/type_set.rb', line 17

def empty?
  set.empty?
end

#merge!(other) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/gelauto/type_set.rb', line 25

def merge!(other)
  other.set.each do |other_ruby_type, other_type|
    if set[other_ruby_type]
      set[other_ruby_type].merge!(other_type)
    else
      set[other_ruby_type] = other_type
    end
  end
end

#sizeObject



13
14
15
# File 'lib/gelauto/type_set.rb', line 13

def size
  set.size
end

#to_sigObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/gelauto/type_set.rb', line 39

def to_sig
  nilable = false

  sigs = set.each_with_object([]) do |(rt, t), ret|
    if rt == ::NilClass
      nilable = true
    else
      ret << t.to_sig
    end
  end

  sigs.uniq!

  if sigs.size == 0
    'T.untyped'
  elsif sigs.size == 1
    sigs.first
  elsif nilable
    "T.nilable(T.any(#{sigs.join(', ')}))"
  else
    "T.any(#{sigs.join(', ')})"
  end
end