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)

Returns:

  • (String)

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

def name
  @name
end

#substringString (readonly)

Returns:

  • (String)

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

def substring
  @substring
end

#subtypesArray<ComplexType> (readonly)

Returns:


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

def subtypes
  @subtypes
end

#tagString (readonly)

Returns:

  • (String)

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

Returns:

  • (Boolean)

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

def defined?
  !undefined?
end

#duck_type?Boolean

Returns:

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

Returns:

  • (Boolean)

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

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

#hash_parameters?Boolean

Returns:

  • (Boolean)

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

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

#key_typesArray<ComplexType>

Returns:


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

def key_types
  @key_types
end

#list_parameters?Boolean

Returns:

  • (Boolean)

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

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

#namespaceString

Returns:

  • (String)

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

Returns:

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

Returns:

  • (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.

Parameters:

  • api_map (ApiMap)

    The ApiMap that performs qualification

  • context (String) (defaults to: '')

    The namespace from which to resolve names

Returns:


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

Returns:

  • (Boolean)

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

def rooted?
  @rooted
end

#scopeSymbol

Returns :class or :instance

Returns:

  • (Symbol)

    :class or :instance


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

Returns:

  • (Boolean)

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

def undefined?
  name == 'undefined'
end

#value_typesArray<ComplexType>

Returns:


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

def value_types
  @subtypes
end

#void?Boolean

Returns:

  • (Boolean)

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

def void?
  name == 'void'
end