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)


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

def name
  @name
end

#substringString (readonly)

Returns:

  • (String)


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

def substring
  @substring
end

#subtypesArray<ComplexType> (readonly)

Returns:



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

def subtypes
  @subtypes
end

#tagString (readonly)

Returns:

  • (String)


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

def tag
  @tag
end

Instance Method Details

#==(other) ⇒ Object



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

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

#defined?Boolean

Returns:

  • (Boolean)


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

def defined?
  !undefined?
end

#duck_type?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/solargraph/complex_type/type_methods.rb', line 21

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

#fixed_parameters?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/solargraph/complex_type/type_methods.rb', line 54

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

#hash_parameters?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/solargraph/complex_type/type_methods.rb', line 59

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

#key_typesArray<ComplexType>

Returns:



69
70
71
# File 'lib/solargraph/complex_type/type_methods.rb', line 69

def key_types
  @key_types
end

#list_parameters?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/solargraph/complex_type/type_methods.rb', line 49

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

#namespaceString

Returns:

  • (String)


74
75
76
77
78
# File 'lib/solargraph/complex_type/type_methods.rb', line 74

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)


26
27
28
29
# File 'lib/solargraph/complex_type/type_methods.rb', line 26

def nil_type?
  @nil_type = (name.casecmp('nil') == 0) if @nil_type.nil?
  @nil_type
end

#parameters?Boolean

Returns:

  • (Boolean)


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

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:



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

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(', ')}>")
  elsif fixed_parameters?
    Solargraph::ComplexType.parse("#{fqns}(#{rtypes.map(&:tag).join(', ')})")
  elsif hash_parameters?
    Solargraph::ComplexType.parse("#{fqns}{#{ltypes.map(&:tag).join(', ')} => #{rtypes.map(&:tag).join(', ')}}")
  else
    Solargraph::ComplexType.parse(fqns)
  end
end

#rooted?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/solargraph/complex_type/type_methods.rb', line 91

def rooted?
  @rooted
end

#scopeSymbol

Returns :class or :instance.

Returns:

  • (Symbol)

    :class or :instance



81
82
83
84
# File 'lib/solargraph/complex_type/type_methods.rb', line 81

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

#undefined?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/solargraph/complex_type/type_methods.rb', line 44

def undefined?
  name == 'undefined'
end

#value_typesArray<ComplexType>

Returns:



64
65
66
# File 'lib/solargraph/complex_type/type_methods.rb', line 64

def value_types
  @subtypes
end

#void?Boolean

Returns:

  • (Boolean)


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

def void?
  name == 'void'
end