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.
Constant Summary collapse
- PARAMETERS_TYPE_BY_STARTING_TAG =
{ '{' => :hash, '(' => :fixed, '<' => :list }.freeze
Instance Attribute Summary collapse
- #name ⇒ String readonly
- #parameters_type ⇒ Symbol? readonly
- #subtypes ⇒ Array<ComplexType> 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
- #generate_substring_from(&to_str) ⇒ String
- #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
- #rooted_substring ⇒ String
- #rooted_tag ⇒ String
-
#scope ⇒ ::Symbol
:class or :instance.
- #substring ⇒ String
- #tag ⇒ String
- #transform(new_name = nil) {|t| ... } ⇒ UniqueType?
- #tuple? ⇒ Boolean
- #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 |
#parameters_type ⇒ Symbol? (readonly)
86 87 88 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 86 def parameters_type @parameters_type end |
#subtypes ⇒ Array<ComplexType> (readonly)
26 27 28 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 26 def subtypes @subtypes end |
Instance Method Details
#==(other) ⇒ Object
175 176 177 178 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 175 def == other return false unless self.class == other.class tag == other.tag end |
#defined? ⇒ Boolean
61 62 63 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 61 def defined? !undefined? end |
#duck_type? ⇒ Boolean
39 40 41 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 39 def duck_type? @duck_type ||= name.start_with?('#') end |
#each_unique_type {|| ... } ⇒ Enumerator<UniqueType>
205 206 207 208 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 205 def each_unique_type &block return enum_for(__method__) unless block_given? yield self end |
#erase_generics(generics_to_erase) ⇒ self
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 71 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
100 101 102 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 100 def fixed_parameters? parameters_type == :fixed end |
#generate_substring_from(&to_str) ⇒ String
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 152 def generate_substring_from(&to_str) key_types_str = key_types.map(&to_str).join(', ') subtypes_str = subtypes.map(&to_str).join(', ') if key_types.none?(&:defined?) && subtypes.none?(&:defined?) '' elsif key_types.empty? && subtypes.empty? '' elsif hash_parameters? "{#{key_types_str} => #{subtypes_str}}" elsif fixed_parameters? "(#{subtypes_str})" else "<#{subtypes_str}>" end end |
#hash_parameters? ⇒ Boolean
105 106 107 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 105 def hash_parameters? parameters_type == :hash end |
#key_types ⇒ Array<ComplexType>
115 116 117 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 115 def key_types @key_types end |
#list_parameters? ⇒ Boolean
95 96 97 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 95 def list_parameters? parameters_type == :list end |
#namespace ⇒ String
120 121 122 123 124 125 126 127 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 120 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
44 45 46 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 44 def nil_type? @nil_type ||= (name.casecmp('nil') == 0) end |
#parameters? ⇒ Boolean
49 50 51 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 49 def parameters? !substring.empty? end |
#qualify(api_map, context = '') ⇒ self, ...
Generate a ComplexType that fully qualifies this type’s namespaces.
189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 189 def qualify api_map, context = '' transform do |t| next t if t.name == GENERIC_TAG_NAME next t if t.duck_type? || t.void? || t.undefined? recon = (t.rooted? ? '' : context) fqns = api_map.qualify(t.name, recon) if fqns.nil? next UniqueType::BOOLEAN if t.tag == 'Boolean' next UniqueType::UNDEFINED end t.recreate(new_name: fqns, make_rooted: true) end end |
#rooted? ⇒ Boolean
180 181 182 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 180 def rooted? @rooted end |
#rooted_name ⇒ String
136 137 138 139 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 136 def rooted_name return name unless rooted? "::#{name}" end |
#rooted_namespace ⇒ String
130 131 132 133 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 130 def rooted_namespace return namespace unless rooted? "::#{namespace}" end |
#rooted_substring ⇒ String
147 148 149 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 147 def rooted_substring @rooted_substring = generate_substring_from(&:rooted_tags) end |
#rooted_tag ⇒ String
34 35 36 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 34 def rooted_tag @rooted_tag ||= rooted_name + rooted_substring end |
#scope ⇒ ::Symbol
Returns :class or :instance.
169 170 171 172 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 169 def scope @scope ||= :instance if duck_type? || nil_type? @scope ||= (name == 'Class' || name == 'Module') && !subtypes.empty? ? :class : :instance end |
#substring ⇒ String
142 143 144 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 142 def substring @substring ||= generate_substring_from(&:tags) end |
#tag ⇒ String
29 30 31 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 29 def tag @tag ||= "#{name}#{substring}" end |
#transform(new_name = nil) {|t| ... } ⇒ UniqueType?
|
|
# File 'lib/solargraph/complex_type/type_methods.rb', line 16
|
#tuple? ⇒ Boolean
53 54 55 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 53 def tuple? @tuple_type ||= (name == 'Tuple') || (name == 'Array' && subtypes.length >= 1 && fixed_parameters?) end |
#undefined? ⇒ Boolean
65 66 67 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 65 def undefined? name == 'undefined' end |
#value_types ⇒ Array<ComplexType>
110 111 112 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 110 def value_types @subtypes end |
#void? ⇒ Boolean
57 58 59 |
# File 'lib/solargraph/complex_type/type_methods.rb', line 57 def void? name == 'void' end |