Class: Tapioca::Dsl::Compilers::Noticed

Inherits:
Tapioca::Dsl::Compiler
  • Object
show all
Defined in:
lib/tapioca/dsl/compilers/noticed.rb

Overview

‘Tapioca::Dsl::Compilers::Noticed` decorates RBI files for subclasses of `Noticed::Event` and `Noticed::Ephemeral`.

For example, with the following notifier class:

~~~rb class NewCommentNotifier < Noticed::Event

required_params :comment
deliver_by :email

end ~~~

This compiler will produce the RBI file ‘new_comment_notifier.rbi` with the following content:

~~~rbi # new_comment_notifier.rbi # typed: true class NewCommentNotifier

class << self
  sig { params(params: T::Hash[Symbol, T.untyped]).returns(NewCommentNotifier) }
  def with(params); end

  sig { params(recipients: T.untyped, enqueue_job: T.nilable(T::Boolean), options: T.untyped).returns(NewCommentNotifier) }
  def deliver(recipients = T.unsafe(nil), enqueue_job: T.unsafe(nil), **options); end

  sig { params(recipients: T.untyped, enqueue_job: T.nilable(T::Boolean), options: T.untyped).returns(NewCommentNotifier) }
  def deliver_later(recipients = T.unsafe(nil), enqueue_job: T.unsafe(nil), **options); end
end

end ~~~

Constant Summary collapse

ConstantType =
type_member do
  { fixed: T.any(T.class_of(::Noticed::Event), T.class_of(::Noticed::Ephemeral)) }
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.gather_constantsObject



47
48
49
50
51
# File 'lib/tapioca/dsl/compilers/noticed.rb', line 47

def gather_constants
  all_classes.select do |klass|
    klass < ::Noticed::Event || klass < ::Noticed::Ephemeral
  end
end

Instance Method Details

#decorateObject

: -> void



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/tapioca/dsl/compilers/noticed.rb', line 56

def decorate
  root.create_path(constant) do |klass|
    singleton = RBI::SingletonClass.new
    klass << singleton

    singleton.create_method(
      "with",
      parameters: [create_param("params", type: "T::Hash[Symbol, T.untyped]")],
      return_type: "::#{constant}",
    )

    singleton.create_method(
      "deliver",
      parameters: [
        create_opt_param("recipients", type: "T.untyped", default: "T.unsafe(nil)"),
        create_kw_opt_param("enqueue_job", type: "T.nilable(T::Boolean)", default: "T.unsafe(nil)"),
        create_kw_rest_param("options", type: "T.untyped"),
      ],
      return_type: "::#{constant}",
    )

    singleton.create_method(
      "deliver_later",
      parameters: [
        create_opt_param("recipients", type: "T.untyped", default: "T.unsafe(nil)"),
        create_kw_opt_param("enqueue_job", type: "T.nilable(T::Boolean)", default: "T.unsafe(nil)"),
        create_kw_rest_param("options", type: "T.untyped"),
      ],
      return_type: "::#{constant}",
    )
  end
end