Class: RbsActivesupport::MethodSearcher

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs_activesupport/method_searcher.rb,
sig/rbs_activesupport/method_searcher.rbs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rbs_builder) ⇒ MethodSearcher

Returns a new instance of MethodSearcher.

Parameters:



10
11
12
# File 'lib/rbs_activesupport/method_searcher.rb', line 10

def initialize(rbs_builder) #: void
  @rbs_builder = rbs_builder
end

Instance Attribute Details

#rbs_builderRBS::DefinitionBuilder (readonly)

: RBS::DefinitionBuilder

Returns:



7
8
9
# File 'lib/rbs_activesupport/method_searcher.rb', line 7

def rbs_builder
  @rbs_builder
end

Instance Method Details

#build_method_def(return_type) ⇒ RBS::MethodType

Parameters:

Returns:



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rbs_activesupport/method_searcher.rb', line 66

def build_method_def(return_type) #: RBS::MethodType
  type = RBS::Types::Function.new(
    required_positionals: [],
    optional_positionals: [],
    rest_positionals: nil,
    trailing_positionals: [],
    required_keywords: {},
    optional_keywords: {},
    rest_keywords: nil,
    return_type:
  )
  RBS::MethodType.new(type_params: [], type:, block: nil, location: nil)
end

#lookup_method_types(type_name, method) ⇒ Array[RBS::MethodType]

Parameters:

Returns:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rbs_activesupport/method_searcher.rb', line 47

def lookup_method_types(type_name, method) #: Array[RBS::MethodType]
  instance = rbs_builder.build_instance(type_name)
  if method.start_with? "@"
    ivar_def = instance.instance_variables[method]
    return [] unless ivar_def

    method_def = build_method_def(ivar_def.type)
    [method_def]
  else
    method_def = instance.methods[method]
    return [] unless method_def

    method_def.defs.map(&:type)
  end
rescue StandardError
  []
end

#method_types_for(delegate) ⇒ Array[String]

Parameters:

Returns:



15
16
17
18
19
20
21
22
23
24
# File 'lib/rbs_activesupport/method_searcher.rb', line 15

def method_types_for(delegate) #: Array[String]
  delegate_to = lookup_method_types(delegate.namespace.to_type_name, delegate.to)
  return ["() -> untyped"] if delegate_to.any? { |t| t.type.return_type.is_a?(RBS::Types::Bases::Any) }

  return_types = return_type_names_for(delegate_to).uniq
                                                   .flat_map { |t| lookup_method_types(t, delegate.method) }
                                                   .map(&:to_s)
  return_types << "() -> untyped" if return_types.empty?
  return_types
end

#return_type_names_for(delegate_to) ⇒ Array[RBS::TypeName]

Parameters:

Returns:



29
30
31
32
33
# File 'lib/rbs_activesupport/method_searcher.rb', line 29

def return_type_names_for(delegate_to) #: Array[RBS::TypeName]
  delegate_to.filter_map do |t|
    type_name_for(t.type.return_type)
  end
end

#type_name_for(type) ⇒ RBS::TypeName?

Parameters:

Returns:



36
37
38
39
40
41
42
43
# File 'lib/rbs_activesupport/method_searcher.rb', line 36

def type_name_for(type) #: RBS::TypeName?
  case type
  when RBS::Types::Optional
    type_name_for(type.type)
  when RBS::Types::ClassSingleton, RBS::Types::ClassInstance, RBS::Types::Interface, RBS::Types::Alias
    type.name
  end
end