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

: (String name, ?superclass_name: String?) ?{ (RBI::Scope scope) -> void } -> Scope



32
33
34
35
36
# File 'lib/tapioca/rbi_ext/model.rb', line 32

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

: (String name, value: String) -> void



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

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

#create_extend(name) ⇒ Object

: (String name) -> void



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

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

#create_include(name) ⇒ Object

: (String name) -> void



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

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

: (String name, ?parameters: Array, ?return_type: String?, ?class_method: bool, ?visibility: RBI::Visibility, ?comments: Array) ?{ (RBI::Method node) -> void } -> void



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/tapioca/rbi_ext/model.rb', line 65

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

: (String name) -> void



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

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

#create_module(name, &block) ⇒ Object

: (String name) ?{ (Scope scope) -> void } -> Scope



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

: (::Module constant) ?{ (Scope scope) -> void } -> Scope



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

: (String name, type: String, ?variance: Symbol, ?fixed: String?, ?upper: String?, ?lower: String?) -> void



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

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