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: nil, class_method: false, visibility: RBI::Public.new, comments: [], &block) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/tapioca/rbi_ext/model.rb', line 90

def create_method(name, parameters: [], return_type: nil, class_method: false, visibility: RBI::Public.new,
  comments: [], &block)
  return unless Tapioca::RBIHelper.valid_method_name?(name)

  sigs = []

  if !block || !parameters.empty? || return_type
    # If there is no block, and the params and return type have not been supplied, then
    # we create a single signature with the given parameters and return type
    params = parameters.map { |param| RBI::SigParam.new(param.param.name.to_s, param.type) }
    sigs << RBI::Sig.new(params: params, return_type: return_type || "T.untyped")
  end

  method = RBI::Method.new(
    name,
    sigs: sigs,
    params: parameters.map(&:param),
    is_singleton: class_method,
    visibility: visibility,
    comments: comments,
    &block
  )
  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_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