Class: Yoda::Typing::Types::Generator
- Inherits:
-
Object
- Object
- Yoda::Typing::Types::Generator
- Defined in:
- lib/yoda/typing/types/generator.rb
Overview
Generator provides construction methods for various type classes.
Instance Attribute Summary collapse
- #environment ⇒ Model::Environment readonly
Instance Method Summary collapse
- #any_type ⇒ Type<RBS::Types::Bases::Any>
- #array_type ⇒ Type<RBS::Types::ClassInstance>
- #boolean_type ⇒ Type<RBS::Types::Bases::Bool>
- #class_class ⇒ Type<RBS::Types::ClassInstance>
- #false_type ⇒ Type<RBS::Types::Literal>
- #float_type ⇒ Type<RBS::Types::ClassInstance>
- #fresh_params_of_method_type(method_type) ⇒ RBS::MethodType
- #function_type(return_type:, required_parameters: [], optional_parameters: [], rest_parameter: nil, post_parameters: [], required_keyword_parameters: [], optional_keyword_parameters: [], keyword_rest_parameter: nil) ⇒ Type<RBS::Types::Function>
- #hash_type ⇒ Type<RBS::Types::ClassInstance>
-
#initialize(environment:) ⇒ Generator
constructor
A new instance of Generator.
- #instance_type(object_class) ⇒ Type<RBS::Types::ClassInstance>
- #instance_type_at(path, args: []) ⇒ Type<RBS::Types::ClassInstance>
- #integer_type(literal = nil) ⇒ Type<RBS::Types::Literal, RBS::Types::ClassInstance>
- #literal_type(literal) ⇒ Type<RBS::Types::Literal>
- #module_class ⇒ Type<RBS::Types::ClassInstance>
- #nil_type ⇒ Type<RBS::Types::Bases::Nil>
- #numeric_type ⇒ Type<RBS::Types::ClassInstance>
- #object_class ⇒ Type<RBS::Types::ClassInstance>
- #object_type ⇒ Type<RBS::Types::ClassInstance>
- #proc_type ⇒ Type<RBS::Types::ClassInstance>
- #range_type ⇒ Type<RBS::Types::ClassInstance>
- #rbs_method_type(**kwargs) ⇒ RBS::MethodType
- #record_type(record) ⇒ Type<RBS::Types::Record>
- #regexp_type ⇒ Type<RBS::Types::ClassInstance>
- #singleton_type(object_class) ⇒ Type<RBS::Types::ClassInstance>
- #singleton_type_at(path) ⇒ Type<RBS::Types::ClassSingleton>
- #standard_error_type ⇒ Type<RBS::Types::ClassInstance>
- #string_type(literal = nil) ⇒ Type<RBS::Types::Literal, RBS::Types::ClassInstance>
- #symbol_type(literal = nil) ⇒ Type<RBS::Types::Literal, RBS::Types::ClassInstance>
- #true_type ⇒ Type<RBS::Types::Literal>
- #union_type(*types) ⇒ Type<RBS::Types::Union>
- #unknown_type(reason: nil) ⇒ Type<RBS::Types::Bases::Any>
- #value_resolve_context(self_type:) ⇒ Model::Environment::ValueResolveContext
- #wrap_rbs_type(rbs_type) ⇒ Type
Constructor Details
#initialize(environment:) ⇒ Generator
Returns a new instance of Generator.
10 11 12 |
# File 'lib/yoda/typing/types/generator.rb', line 10 def initialize(environment:) @environment = environment end |
Instance Attribute Details
#environment ⇒ Model::Environment (readonly)
7 8 9 |
# File 'lib/yoda/typing/types/generator.rb', line 7 def environment @environment end |
Instance Method Details
#any_type ⇒ Type<RBS::Types::Bases::Any>
140 141 142 |
# File 'lib/yoda/typing/types/generator.rb', line 140 def any_type wrap_rbs_type(RBS::Types::Bases::Any.new(location: nil)) end |
#array_type ⇒ Type<RBS::Types::ClassInstance>
73 74 75 |
# File 'lib/yoda/typing/types/generator.rb', line 73 def array_type instance_type_at('::Array') end |
#boolean_type ⇒ Type<RBS::Types::Bases::Bool>
27 28 29 |
# File 'lib/yoda/typing/types/generator.rb', line 27 def boolean_type wrap_rbs_type(RBS::Types::Bases::Bool.new(location: nil)) end |
#class_class ⇒ Type<RBS::Types::ClassInstance>
152 153 154 |
# File 'lib/yoda/typing/types/generator.rb', line 152 def class_class singleton_type_at('::Class') end |
#false_type ⇒ Type<RBS::Types::Literal>
37 38 39 |
# File 'lib/yoda/typing/types/generator.rb', line 37 def false_type literal_type(false) end |
#float_type ⇒ Type<RBS::Types::ClassInstance>
107 108 109 |
# File 'lib/yoda/typing/types/generator.rb', line 107 def float_type instance_type_at('::Float') end |
#fresh_params_of_method_type(method_type) ⇒ RBS::MethodType
244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/yoda/typing/types/generator.rb', line 244 def fresh_params_of_method_type(method_type) new_type_params = method_type.type_params.map(&method(:append_id_to_type_var)) new_type_variables = RBS::Types::Variable.build(new_type_params) subst = RBS::Substitution.build(method_type.type_params, new_type_variables) method_type.update( type_params: new_type_params, type: method_type.type.sub(subst), block: method_type.block&.sub(subst), ) end |
#function_type(return_type:, required_parameters: [], optional_parameters: [], rest_parameter: nil, post_parameters: [], required_keyword_parameters: [], optional_keyword_parameters: [], keyword_rest_parameter: nil) ⇒ Type<RBS::Types::Function>
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/yoda/typing/types/generator.rb', line 199 def function_type( return_type:, required_parameters: [], optional_parameters: [], rest_parameter: nil, post_parameters: [], required_keyword_parameters: [], optional_keyword_parameters: [], keyword_rest_parameter: nil ) build_param = lambda { |type| RBS::Types::Function::Param.new(type: unwrap(type), name: nil) } build_keyword = lambda { |(name, type)| [name.to_sym, RBS::Types::Function::Param.new(type: unwrap(type), name: name.to_sym)] } function_type = RBS::Types::Function.new( required_positionals: required_parameters.map(&build_param), optional_positionals: optional_parameters.map(&build_param), rest_positionals: rest_parameter&.yield_self(&build_param), trailing_positionals: post_parameters.map(&build_param), required_keywords: required_keyword_parameters.map(&build_keyword).to_h, optional_keywords: optional_keyword_parameters.map(&build_keyword).to_h, rest_keywords: keyword_rest_parameter&.yield_self(&build_param), return_type: unwrap(return_type), ) wrap_rbs_type(function_type) end |
#hash_type ⇒ Type<RBS::Types::ClassInstance>
78 79 80 |
# File 'lib/yoda/typing/types/generator.rb', line 78 def hash_type instance_type_at('::Hash') end |
#instance_type(object_class) ⇒ Type<RBS::Types::ClassInstance>
129 130 131 |
# File 'lib/yoda/typing/types/generator.rb', line 129 def instance_type(object_class) instance_type_at(object_class.path) end |
#instance_type_at(path, args: []) ⇒ Type<RBS::Types::ClassInstance>
173 174 175 176 |
# File 'lib/yoda/typing/types/generator.rb', line 173 def instance_type_at(path, args: []) name = to_type_name(path) name ? wrap_rbs_type(RBS::Types::ClassInstance.new(name: name, args: args.map(&method(:unwrap)), location: nil)) : unknown_type(reason: "#{path} does not exists") end |
#integer_type(literal = nil) ⇒ Type<RBS::Types::Literal, RBS::Types::ClassInstance>
98 99 100 101 102 103 104 |
# File 'lib/yoda/typing/types/generator.rb', line 98 def integer_type(literal = nil) if literal literal_type(literal.to_i) else instance_type_at('::Integer') end end |
#literal_type(literal) ⇒ Type<RBS::Types::Literal>
68 69 70 |
# File 'lib/yoda/typing/types/generator.rb', line 68 def literal_type(literal) wrap_rbs_type(RBS::Types::Literal.new(literal: literal, location: nil)) end |
#module_class ⇒ Type<RBS::Types::ClassInstance>
157 158 159 |
# File 'lib/yoda/typing/types/generator.rb', line 157 def module_class singleton_type_at('::Module') end |
#nil_type ⇒ Type<RBS::Types::Bases::Nil>
42 43 44 |
# File 'lib/yoda/typing/types/generator.rb', line 42 def nil_type wrap_rbs_type(RBS::Types::Bases::Nil.new(location: nil)) end |
#numeric_type ⇒ Type<RBS::Types::ClassInstance>
112 113 114 |
# File 'lib/yoda/typing/types/generator.rb', line 112 def numeric_type instance_type_at('::Numeric') end |
#object_class ⇒ Type<RBS::Types::ClassInstance>
162 163 164 |
# File 'lib/yoda/typing/types/generator.rb', line 162 def object_class singleton_type_at('::Object') end |
#object_type ⇒ Type<RBS::Types::ClassInstance>
117 118 119 |
# File 'lib/yoda/typing/types/generator.rb', line 117 def object_type instance_type_at('::Object') end |
#proc_type ⇒ Type<RBS::Types::ClassInstance>
93 94 95 |
# File 'lib/yoda/typing/types/generator.rb', line 93 def proc_type instance_type_at('::Proc') end |
#range_type ⇒ Type<RBS::Types::ClassInstance>
83 84 85 |
# File 'lib/yoda/typing/types/generator.rb', line 83 def range_type instance_type_at('::Range') end |
#rbs_method_type(**kwargs) ⇒ RBS::MethodType
233 234 235 236 237 238 239 240 |
# File 'lib/yoda/typing/types/generator.rb', line 233 def rbs_method_type(**kwargs) RBS::MethodType.new( type_params: [], type: unwrap(function_type(**kwargs)), block: nil, location: nil, ) end |
#record_type(record) ⇒ Type<RBS::Types::Record>
135 136 137 |
# File 'lib/yoda/typing/types/generator.rb', line 135 def record_type(record) wrap_rbs_type(RBS::Types::Record.new(fields: record, location: nil)) end |
#regexp_type ⇒ Type<RBS::Types::ClassInstance>
88 89 90 |
# File 'lib/yoda/typing/types/generator.rb', line 88 def regexp_type instance_type_at('::RegExp') end |
#singleton_type(object_class) ⇒ Type<RBS::Types::ClassInstance>
123 124 125 |
# File 'lib/yoda/typing/types/generator.rb', line 123 def singleton_type(object_class) singleton_type_at(object_class.path) end |
#singleton_type_at(path) ⇒ Type<RBS::Types::ClassSingleton>
179 180 181 182 |
# File 'lib/yoda/typing/types/generator.rb', line 179 def singleton_type_at(path) name = to_type_name(path) name ? wrap_rbs_type(RBS::Types::ClassSingleton.new(name: name, location: nil)) : unknown_type(reason: "#{path} does not exists") end |
#standard_error_type ⇒ Type<RBS::Types::ClassInstance>
167 168 169 |
# File 'lib/yoda/typing/types/generator.rb', line 167 def standard_error_type instance_type_at('::StandardError') end |
#string_type(literal = nil) ⇒ Type<RBS::Types::Literal, RBS::Types::ClassInstance>
48 49 50 51 52 53 54 |
# File 'lib/yoda/typing/types/generator.rb', line 48 def string_type(literal = nil) if literal literal_type(literal.to_s) else instance_type_at('::String') end end |
#symbol_type(literal = nil) ⇒ Type<RBS::Types::Literal, RBS::Types::ClassInstance>
58 59 60 61 62 63 64 |
# File 'lib/yoda/typing/types/generator.rb', line 58 def symbol_type(literal = nil) if literal literal_type(literal.to_sym) else instance_type_at('::Symbol') end end |
#true_type ⇒ Type<RBS::Types::Literal>
32 33 34 |
# File 'lib/yoda/typing/types/generator.rb', line 32 def true_type literal_type(true) end |
#union_type(*types) ⇒ Type<RBS::Types::Union>
186 187 188 |
# File 'lib/yoda/typing/types/generator.rb', line 186 def union_type(*types) wrap_rbs_type(RBS::Types::Union.new(types: types.map(&method(:unwrap)), location: nil)) end |
#unknown_type(reason: nil) ⇒ Type<RBS::Types::Bases::Any>
146 147 148 149 |
# File 'lib/yoda/typing/types/generator.rb', line 146 def unknown_type(reason: nil) Logger.trace("Use unknown type because #{reason}") if reason any_type end |
#value_resolve_context(self_type:) ⇒ Model::Environment::ValueResolveContext
16 17 18 |
# File 'lib/yoda/typing/types/generator.rb', line 16 def value_resolve_context(self_type:) Model::Environment::ValueResolveContext.new(self_type: self_type.rbs_type) end |