Module: Solargraph::ComplexType::TypeMethods Abstract
- Included in:
- UniqueType
- Defined in:
- lib/solargraph/complex_type/type_methods.rb
Overview
This module is abstract.
This mixin relies on these - instance variables:
@name: String
@subtypes: Array<ComplexType>
@rooted: boolish
methods:
transform()
Methods for accessing type data available from both ComplexType and UniqueType.
Instance Attribute Summary collapse
- #name ⇒ String readonly
- #substring ⇒ String readonly
- #subtypes ⇒ Array<ComplexType> readonly
- #tag ⇒ String readonly
Instance Method Summary collapse
- #==(other) ⇒ Object
- #defined? ⇒ Boolean
- #duck_type? ⇒ Boolean
- #each_unique_type {|| ... } ⇒ Enumerator<UniqueType>
- #erase_generics(generics_to_erase) ⇒ self
- #fixed_parameters? ⇒ Boolean
- #hash_parameters? ⇒ Boolean
- #key_types ⇒ Array<ComplexType>
- #list_parameters? ⇒ Boolean
- #namespace ⇒ String
- #nil_type? ⇒ Boolean
- #parameters? ⇒ Boolean
-
#qualify(api_map, context = '') ⇒ self, ...
Generate a ComplexType that fully qualifies this type’s namespaces.
- #rooted? ⇒ Boolean
- #rooted_name ⇒ String
- #rooted_namespace ⇒ String
-
#scope ⇒ ::Symbol
:class or :instance.
- #transform(new_name = nil) {|t| ... } ⇒ UniqueType?
- #undefined? ⇒ Boolean
- #value_types ⇒ Array<ComplexType>
- #void? ⇒ Boolean
Instance Attribute Details
#name ⇒ String (readonly)
23 24 25 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 23 def name @name end |
#substring ⇒ String (readonly)
26 27 28 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 26 def substring @substring end |
#subtypes ⇒ Array<ComplexType> (readonly)
32 33 34 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 32 def subtypes @subtypes end |
#tag ⇒ String (readonly)
29 30 31 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 29 def tag @tag end |
Instance Method Details
#==(other) ⇒ Object
131 132 133 134 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 131 def == other return false unless self.class == other.class tag == other.tag end |
#defined? ⇒ Boolean
53 54 55 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 53 def defined? !undefined? end |
#duck_type? ⇒ Boolean
35 36 37 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 35 def duck_type? @duck_type ||= name.start_with?('#') end |
#each_unique_type {|| ... } ⇒ Enumerator<UniqueType>
173 174 175 176 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 173 def each_unique_type &block return enum_for(__method__) unless block_given? yield self end |
#erase_generics(generics_to_erase) ⇒ self
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 63 def erase_generics(generics_to_erase) transform do |type| if type.name == ComplexType::GENERIC_TAG_NAME if type.all_params.length == 1 && generics_to_erase.include?(type.all_params.first.to_s) ComplexType::UNDEFINED else type end else type end end end |
#fixed_parameters? ⇒ Boolean
83 84 85 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 83 def fixed_parameters? substring.start_with?('(') end |
#hash_parameters? ⇒ Boolean
88 89 90 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 88 def hash_parameters? substring.start_with?('{') end |
#key_types ⇒ Array<ComplexType>
98 99 100 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 98 def key_types @key_types end |
#list_parameters? ⇒ Boolean
78 79 80 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 78 def list_parameters? substring.start_with?('<') end |
#namespace ⇒ String
103 104 105 106 107 108 109 110 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 103 def namespace # if priority higher than ||=, old implements cause unnecessary check @namespace ||= lambda do return 'Object' if duck_type? return 'NilClass' if nil_type? return (name == 'Class' || name == 'Module') && !subtypes.empty? ? subtypes.first.name : name end.call end |
#nil_type? ⇒ Boolean
40 41 42 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 40 def nil_type? @nil_type ||= (name.casecmp('nil') == 0) end |
#parameters? ⇒ Boolean
45 46 47 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 45 def parameters? !substring.empty? end |
#qualify(api_map, context = '') ⇒ self, ...
Generate a ComplexType that fully qualifies this type’s namespaces.
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 145 def qualify api_map, context = '' return self if name == GENERIC_TAG_NAME 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 all_ltypes = key_types.map { |t| t.qualify api_map, context }.uniq all_rtypes = value_types.map { |t| t.qualify api_map, context } if list_parameters? rtypes = all_rtypes.uniq Solargraph::ComplexType.parse("#{fqns}<#{rtypes.map(&:tag).join(', ')}>") elsif fixed_parameters? Solargraph::ComplexType.parse("#{fqns}(#{all_rtypes.map(&:tag).join(', ')})") elsif hash_parameters? ltypes = all_ltypes.uniq rtypes = all_rtypes.uniq Solargraph::ComplexType.parse("#{fqns}{#{ltypes.map(&:tag).join(', ')} => #{rtypes.map(&:tag).join(', ')}}") else Solargraph::ComplexType.parse(fqns) end end |
#rooted? ⇒ Boolean
136 137 138 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 136 def rooted? @rooted end |
#rooted_name ⇒ String
119 120 121 122 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 119 def rooted_name return name unless rooted? "::#{name}" end |
#rooted_namespace ⇒ String
113 114 115 116 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 113 def rooted_namespace return namespace unless rooted? "::#{namespace}" end |
#scope ⇒ ::Symbol
Returns :class or :instance.
125 126 127 128 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 125 def scope @scope ||= :instance if duck_type? || nil_type? @scope ||= (name == 'Class' || name == 'Module') && !subtypes.empty? ? :class : :instance end |
#transform(new_name = nil) {|t| ... } ⇒ UniqueType?
|
|
# File 'lib/solargraph/complex_type/type_methods.rb', line 16
|
#undefined? ⇒ Boolean
57 58 59 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 57 def undefined? name == 'undefined' end |
#value_types ⇒ Array<ComplexType>
93 94 95 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 93 def value_types @subtypes end |
#void? ⇒ Boolean
49 50 51 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 49 def void? name == 'void' end |