Class: Yoda::Model::Environment::ValueFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/yoda/model/environment/value_factory.rb

Overview

Environment can instanciate Values::Base objects from rbs types

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environment:) ⇒ ValueFactory

Returns a new instance of ValueFactory.

Parameters:



17
18
19
# File 'lib/yoda/model/environment/value_factory.rb', line 17

def initialize(environment:)
  @environment = environment
end

Instance Attribute Details

#environmentEnvironment (readonly)

Returns:



9
10
11
# File 'lib/yoda/model/environment/value_factory.rb', line 9

def environment
  @environment
end

Class Method Details

.from_environment(environment) ⇒ Object

Parameters:



12
13
14
# File 'lib/yoda/model/environment/value_factory.rb', line 12

def self.from_environment(environment)
  new(environment: environment)
end

Instance Method Details

#resolve_value_by_rbs_type(type, context: ValueResolveContext.empty) ⇒ Values::Base?

Parameters:

  • type (RBS::Types::Bases::Base, RBS::Types::Variable, RBS::Types::ClassSingleton, RBS::Types::Interface, RBS::Types::ClassInstance, RBS::Types::Alias, RBS::Types::Tuple, RBS::Types::Record, RBS::Types::Optional, RBS::Types::Union, RBS::Types::Intersection, RBS::Types::Function, RBS::Types::Block, RBS::Types::Proc, RBS::Types::Literal, ValueResolveContext::WrappedType)

Returns:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/yoda/model/environment/value_factory.rb', line 23

def resolve_value_by_rbs_type(type, context: ValueResolveContext.empty)
  return resolve_value_by_rbs_type(type.wrapped_type, context: type.context) if type.respond_to?(:act_as_type_wrapper?)

  case type
  when RBS::Types::Bases::Any
    Values::EmptyValue.new
  when RBS::Types::Bases::Top
    # TODO: Implement as everything.
    Values::EmptyValue.new
  when RBS::Types::Bases::Bottom
    Values::EmptyValue.new
  when RBS::Types::Bases::Void
    Values::EmptyValue.new
  when RBS::Types::Bases::Bool
    Values::UnionValue.new(
      resolve_instance_by_rbs_type_name(TypeName("::TrueClass")),
      resolve_instance_by_rbs_type_name(TypeName("::FalseClass")),
    )
  when RBS::Types::Bases::Nil
    resolve_instance_by_rbs_type_name(TypeName("::NilClass"))
  when RBS::Types::ClassSingleton
    resolve_singleton_by_rbs_type_name(type.name)
  when RBS::Types::ClassInstance
    resolve_instance_by_rbs_type_name(type.name, args: type.args)
  when RBS::Types::Interface
    args = type.args.map {|arg| resolve_value_by_rbs_type(arg, context: context) }

    resolve_interface_by_rbs_type_name(type.name, args: type.args)
  when RBS::Types::Union
    Values::UnionValue.new(
      *type.types.flat_map { |ty| resolve_value_by_rbs_type(ty, context: context) }
    )
  when RBS::Types::Optional
    Values::UnionValue.new(
      resolve_value_by_rbs_type(type.type),
      resolve_instance_by_rbs_type_name(TypeName("::NilClass")),
    )
  when RBS::Types::Intersection
    Values::IntersectionValue.new(
      *type.types.map { |ty| resolve_value_by_rbs_type(ty, context: context) }
    )
  when RBS::Types::Literal
    literal_instance(type.literal)
  when RBS::Types::Tuple
    # TODO: Implement as tuple
    # Values::Tuple.new(value_lists: type.types.map { |ty| resolve_value_by_rbs_type(ty) })

    resolve_instance_by_rbs_type_name(TypeName("::Array"))
  when RBS::Types::Record
    # TODO: Implement as record
    # Values::Tuple.new(values_map: type.fields.map { |(key, value)| [key, resolve_value_by_rbs_type(value)] }.to_h)
    resolve_instance_by_rbs_type_name(TypeName("::Hash"))
  when RBS::Types::Proc
    # TODO: Implement as proc
    # Values::Proc.new("::Proc", type_args: type)
    resolve_instance_by_rbs_type_name(TypeName("::Proc"))
  when RBS::Types::Alias
    rbs_alias = environment.rbs_environment.alias_decls[type.name]&.decl

    if rbs_alias
      resolve_value_by_rbs_type(rbs_alias.type)
    else
      Values::EmptyValue.new
    end
  when RBS::Types::Variable
    Logger.warn("Value factory does not has proper information to convert #{type}.")
    Values::EmptyValue.new
  when RBS::Types::Bases::Self
    if context.self_type
      resolve_value_by_rbs_type(context.self_type)
    else
      Logger.warn("Value factory does not has proper information to convert #{type}.")
      Values::EmptyValue.new
    end
  when RBS::Types::Bases::Class
    if context.self_type
      resolve_value_by_rbs_type(context.self_type).singleton_class_value
    else
      Logger.warn("Value factory does not has proper information to convert #{type}.")
      Values::EmptyValue.new
    end
  when RBS::Types::Bases::Instance
    if context.self_type
      resolve_value_by_rbs_type(context.self_type).instance_value
    else
      Logger.warn("Value factory does not has proper information to convert #{type}.")
      Values::EmptyValue.new
    end
  else
    raise "Unexpected type given: #{type}"
  end
end