Module: T::DefMods

Included in:
Syntax
Defined in:
lib/types/def_mods.rb

Overview

Optional mixin providing ‘abstract`, `override`, `overridable`, and `final` as method-level DSL keywords. Use with `extend T::DefMods`.

These are alternatives to writing modifiers inside a ‘sig { … }` block:

sig { void }
abstract def foo; end

is equivalent to:

sig { abstract.void }
def foo; end

They all return the method name, so that they can be chained with methods like ‘private`. However, unlike those methods, these methods use the `sig` declaration to discover the most-recently-defined method, instead of needing `*_class_method` variants, like `private_class_method`.

Instance Method Summary collapse

Instance Method Details

#abstract(method_name) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/types/def_mods.rb', line 22

def abstract(method_name)
  Kernel.raise TypeError.new("abstract accepts a Symbol, got #{method_name.class}") unless method_name.is_a?(Symbol)

  begin
    T::Private::Methods.declare_abstract(T.unsafe(self), method_name)
  rescue T::Private::Methods::DeclBuilder::BuilderError => e
    T::Configuration.sig_builder_error_handler(e, Kernel.caller_locations(1, 1)&.first)
  end

  method_name
end

#final(method_name) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/types/def_mods.rb', line 46

def final(method_name)
  Kernel.raise TypeError.new("final accepts a Symbol, got #{method_name.class}") unless method_name.is_a?(Symbol)

  begin
    T::Private::Methods.declare_final(T.unsafe(self), method_name)
  rescue T::Private::Methods::DeclBuilder::BuilderError => e
    T::Configuration.sig_builder_error_handler(e, Kernel.caller_locations(1, 1)&.first)
  end

  method_name
end

#overridable(method_name) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/types/def_mods.rb', line 58

def overridable(method_name)
  Kernel.raise TypeError.new("overridable accepts a Symbol, got #{method_name.class}") unless method_name.is_a?(Symbol)

  begin
    T::Private::Methods.declare_overridable(T.unsafe(self), method_name)
  rescue T::Private::Methods::DeclBuilder::BuilderError => e
    T::Configuration.sig_builder_error_handler(e, Kernel.caller_locations(1, 1)&.first)
  end

  method_name
end

#override(method_name, allow_incompatible: false) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/types/def_mods.rb', line 34

def override(method_name, allow_incompatible: false)
  Kernel.raise TypeError.new("override accepts a Symbol, got #{method_name.class}") unless method_name.is_a?(Symbol)

  begin
    T::Private::Methods.declare_override(T.unsafe(self), method_name, allow_incompatible: allow_incompatible)
  rescue T::Private::Methods::DeclBuilder::BuilderError => e
    T::Configuration.sig_builder_error_handler(e, Kernel.caller_locations(1, 1)&.first)
  end

  method_name
end