Module: Solargraph::ComplexType::TypeMethods

Included in:
UniqueType
Defined in:
lib/solargraph/complex_type/type_methods.rb

Overview

Methods for accessing type data.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nameString (readonly)


7
8
9
# File 'lib/solargraph/complex_type/type_methods.rb', line 7

def name
  @name
end

#substringString (readonly)


10
11
12
# File 'lib/solargraph/complex_type/type_methods.rb', line 10

def substring
  @substring
end

#subtypesArray<ComplexType> (readonly)


16
17
18
# File 'lib/solargraph/complex_type/type_methods.rb', line 16

def subtypes
  @subtypes
end

#tagString (readonly)


13
14
15
# File 'lib/solargraph/complex_type/type_methods.rb', line 13

def tag
  @tag
end

Instance Method Details

#==(other) ⇒ Object


83
84
85
86
# File 'lib/solargraph/complex_type/type_methods.rb', line 83

def == other
  return false unless self.class == other.class
  tag == other.tag
end

#defined?Boolean


37
38
39
# File 'lib/solargraph/complex_type/type_methods.rb', line 37

def defined?
  !undefined?
end

#duck_type?Boolean


19
20
21
# File 'lib/solargraph/complex_type/type_methods.rb', line 19

def duck_type?
  @duck_type ||= name.start_with?('#')
end

#fixed_parameters?Boolean


51
52
53
# File 'lib/solargraph/complex_type/type_methods.rb', line 51

def fixed_parameters?
  substring.start_with?('(')
end

#hash_parameters?Boolean


56
57
58
# File 'lib/solargraph/complex_type/type_methods.rb', line 56

def hash_parameters?
  substring.start_with?('{')
end

#key_typesArray<ComplexType>


66
67
68
# File 'lib/solargraph/complex_type/type_methods.rb', line 66

def key_types
  @key_types
end

#list_parameters?Boolean


46
47
48
# File 'lib/solargraph/complex_type/type_methods.rb', line 46

def list_parameters?
  substring.start_with?('<')
end

#namespaceString


71
72
73
74
75
# File 'lib/solargraph/complex_type/type_methods.rb', line 71

def namespace
  @namespace ||= 'Object' if duck_type?
  @namespace ||= 'NilClass' if nil_type?
  @namespace ||= (name == 'Class' || name == 'Module') && !subtypes.empty? ? subtypes.first.name : name
end

#nil_type?Boolean


24
25
26
# File 'lib/solargraph/complex_type/type_methods.rb', line 24

def nil_type?
  @nil_type ||= (name.downcase == 'nil')
end

#parameters?Boolean


29
30
31
# File 'lib/solargraph/complex_type/type_methods.rb', line 29

def parameters?
  !substring.empty?
end

#qualify(api_map, context = '') ⇒ ComplexType

Generate a ComplexType that fully qualifies this type's namespaces.


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/solargraph/complex_type/type_methods.rb', line 97

def qualify api_map, context = ''
  return ComplexType.new([self]) if duck_type? || void? || undefined?
  recon = (rooted? ? '' : context)
  fqns = api_map.qualify(name, recon)
  if fqns.nil?
    return UniqueType::BOOLEAN if tag == 'Boolean'
    return UniqueType::UNDEFINED
  end
  fqns = "::#{fqns}" # Ensure the resulting complex type is rooted
  ltypes = key_types.map do |t|
    t.qualify api_map, context
  end
  rtypes = value_types.map do |t|
    t.qualify api_map, context
  end
  if list_parameters?
    Solargraph::ComplexType.parse("#{fqns}<#{rtypes.map(&:tag).join(', ')}>").first
  elsif fixed_parameters?
    Solargraph::ComplexType.parse("#{fqns}(#{rtypes.map(&:tag).join(', ')})").first
  elsif hash_parameters?
    Solargraph::ComplexType.parse("#{fqns}{#{ltypes.map(&:tag).join(', ')} => #{rtypes.map(&:tag).join(', ')}}").first
  else
    Solargraph::ComplexType.parse(fqns).first
  end
end

#rooted?Boolean


88
89
90
# File 'lib/solargraph/complex_type/type_methods.rb', line 88

def rooted?
  @rooted
end

#scopeSymbol


78
79
80
81
# File 'lib/solargraph/complex_type/type_methods.rb', line 78

def scope
  @scope ||= :instance if duck_type? || nil_type?
  @scope ||= (name == 'Class' || name == 'Module') && !subtypes.empty? ? :class : :instance
end

#undefined?Boolean


41
42
43
# File 'lib/solargraph/complex_type/type_methods.rb', line 41

def undefined?
  name == 'undefined'
end

#value_typesArray<ComplexType>


61
62
63
# File 'lib/solargraph/complex_type/type_methods.rb', line 61

def value_types
  @subtypes
end

#void?Boolean


33
34
35
# File 'lib/solargraph/complex_type/type_methods.rb', line 33

def void?
  name == 'void'
end