Class: RBI::Tree

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/tapioca/rbi_ext/model.rb

Instance Method Summary collapse

Instance Method Details

#create_class(name, superclass_name: nil, &block) ⇒ Object



38
39
40
41
42
# File 'lib/tapioca/rbi_ext/model.rb', line 38

def create_class(name, superclass_name: nil, &block)
  T.cast(create_node(RBI::Class.new(name, superclass_name: superclass_name)), RBI::Scope).tap do |node|
    block&.call(node)
  end
end

#create_constant(name, value:) ⇒ Object



45
46
47
# File 'lib/tapioca/rbi_ext/model.rb', line 45

def create_constant(name, value:)
  create_node(RBI::Const.new(name, value))
end

#create_extend(name) ⇒ Object



55
56
57
# File 'lib/tapioca/rbi_ext/model.rb', line 55

def create_extend(name)
  create_node(RBI::Extend.new(name))
end

#create_include(name) ⇒ Object



50
51
52
# File 'lib/tapioca/rbi_ext/model.rb', line 50

def create_include(name)
  create_node(RBI::Include.new(name))
end

#create_method(name, parameters: [], return_type: "T.untyped", class_method: false, visibility: RBI::Public.new, comments: []) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/tapioca/rbi_ext/model.rb', line 89

def create_method(name, parameters: [], return_type: "T.untyped", class_method: false, visibility: RBI::Public.new,
  comments: [])
  sig_params = parameters.to_h { |param| [param.param.name, param.type] }
  sig = create_sig(parameters: sig_params, return_type: return_type)
  create_method_with_sigs(
    name,
    sigs: [sig],
    parameters: parameters.map(&:param),
    class_method: class_method,
    visibility: visibility,
    comments: comments,
  )
end

#create_method_with_sigs(name, sigs:, parameters: [], class_method: false, visibility: RBI::Public.new, comments: []) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/tapioca/rbi_ext/model.rb', line 113

def create_method_with_sigs(name, sigs:, parameters: [], class_method: false, visibility: RBI::Public.new,
  comments: [])
  return unless Tapioca::RBIHelper.valid_method_name?(name)

  method = RBI::Method.new(
    name,
    sigs: sigs,
    params: parameters,
    is_singleton: class_method,
    visibility: visibility,
    comments: comments,
  )
  self << method
end

#create_mixes_in_class_methods(name) ⇒ Object



60
61
62
# File 'lib/tapioca/rbi_ext/model.rb', line 60

def create_mixes_in_class_methods(name)
  create_node(RBI::MixesInClassMethods.new(name))
end

#create_module(name, &block) ⇒ Object



25
26
27
28
29
# File 'lib/tapioca/rbi_ext/model.rb', line 25

def create_module(name, &block)
  T.cast(create_node(RBI::Module.new(name)), RBI::Scope).tap do |node|
    block&.call(node)
  end
end

#create_path(constant, &block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/tapioca/rbi_ext/model.rb', line 9

def create_path(constant, &block)
  constant_name = Tapioca::Runtime::Reflection.name_of(constant)
  raise "given constant does not have a name" unless constant_name

  instance = ::Module.const_get(constant_name)
  case instance
  when ::Class
    create_class(constant.to_s, &block)
  when ::Module
    create_module(constant.to_s, &block)
  else
    raise "unexpected type: #{constant_name} is a #{instance.class}"
  end
end

#create_sig(parameters:, return_type: "T.untyped") ⇒ Object



134
135
136
137
138
139
# File 'lib/tapioca/rbi_ext/model.rb', line 134

def create_sig(parameters:, return_type: "T.untyped")
  params = parameters.map do |name, type|
    RBI::SigParam.new(name.to_s, type)
  end
  RBI::Sig.new(params: params, return_type: return_type)
end

#create_type_variable(name, type:, variance: :invariant, fixed: nil, upper: nil, lower: nil) ⇒ Object



74
75
76
77
# File 'lib/tapioca/rbi_ext/model.rb', line 74

def create_type_variable(name, type:, variance: :invariant, fixed: nil, upper: nil, lower: nil)
  value = Tapioca::RBIHelper.serialize_type_variable(type, variance, fixed, upper, lower)
  create_node(RBI::TypeMember.new(name, value))
end