Class: T::Private::Methods::DeclBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/types/private/methods/decl_builder.rb

Defined Under Namespace

Classes: BuilderError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mod) ⇒ DeclBuilder

Returns a new instance of DeclBuilder.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/types/private/methods/decl_builder.rb', line 18

def initialize(mod)
  # TODO RUBYPLAT-1278 - with ruby 2.5, use kwargs here
  @decl = Declaration.new(
    mod,
    ARG_NOT_PROVIDED, # params
    ARG_NOT_PROVIDED, # returns
    ARG_NOT_PROVIDED, # bind
    Modes.standard, # mode
    ARG_NOT_PROVIDED, # checked
    false, # finalized
    ARG_NOT_PROVIDED, # soft_notify
    nil, # override_allow_incompatible
    ARG_NOT_PROVIDED, # type_parameters
    ARG_NOT_PROVIDED, # generated
  )
end

Instance Attribute Details

#declObject (readonly)

Returns the value of attribute decl.



8
9
10
# File 'lib/types/private/methods/decl_builder.rb', line 8

def decl
  @decl
end

Instance Method Details

#abstractObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/types/private/methods/decl_builder.rb', line 154

def abstract
  check_live!

  case decl.mode
  when Modes.standard
    decl.mode = Modes.abstract
  when Modes.abstract
    raise BuilderError.new(".abstract cannot be repeated in a single signature")
  else
    raise BuilderError.new("`.abstract` cannot be combined with any of `.override`, `.implementation`, or "\
          "`.overridable`.")
  end

  self
end

#bind(type) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/types/private/methods/decl_builder.rb', line 71

def bind(type)
  check_live!
  if !decl.bind.equal?(ARG_NOT_PROVIDED)
    raise BuilderError.new("You can't call .bind multiple times in a signature.")
  end

  decl.bind = type

  self
end

#checked(level) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/types/private/methods/decl_builder.rb', line 82

def checked(level)
  if T.unsafe(true)
    raise "The .checked API is unstable, so we don't want it used until we redesign it. To change Sorbet's runtime behavior, see https://sorbet.org/docs/tconfiguration"
  end
  check_live!

  if !decl.checked.equal?(ARG_NOT_PROVIDED)
    raise BuilderError.new("You can't call .checked multiple times in a signature.")
  end
  if !decl.soft_notify.equal?(ARG_NOT_PROVIDED)
    raise BuilderError.new("You can't use .checked with .soft.")
  end
  if !decl.generated.equal?(ARG_NOT_PROVIDED)
    raise BuilderError.new("You can't use .checked with .generated.")
  end
  if !T::Private::RuntimeLevels::LEVELS.include?(level)
    raise BuilderError.new("Invalid `checked` level '#{level}'. Use one of: #{T::Private::RuntimeLevels::LEVELS}.")
  end

  decl.checked = level

  self
end

#finalize!Object



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/types/private/methods/decl_builder.rb', line 248

def finalize!
  check_live!

  if decl.bind.equal?(ARG_NOT_PROVIDED)
    decl.bind = nil
  end
  if decl.checked.equal?(ARG_NOT_PROVIDED)
    decl.checked = :always
  end
  if decl.soft_notify.equal?(ARG_NOT_PROVIDED)
    decl.soft_notify = nil
  end
  if decl.generated.equal?(ARG_NOT_PROVIDED)
    decl.generated = false
  end
  if decl.params.equal?(ARG_NOT_PROVIDED)
    decl.params = {}
  end
  if decl.type_parameters.equal?(ARG_NOT_PROVIDED)
    decl.type_parameters = {}
  end

  decl.finalized = true

  self
end

#generatedObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/types/private/methods/decl_builder.rb', line 133

def generated
  if T.unsafe(true)
    raise "The .generated API is unstable, so we don't want it used until we redesign it. To change Sorbet's runtime behavior, see https://sorbet.org/docs/tconfiguration"
  end
  check_live!

  if !decl.generated.equal?(ARG_NOT_PROVIDED)
    raise BuilderError.new("You can't call .generated multiple times in a signature.")
  end
  if !decl.checked.equal?(ARG_NOT_PROVIDED)
    raise BuilderError.new("You can't use .generated with .checked.")
  end
  if !decl.soft_notify.equal?(ARG_NOT_PROVIDED)
    raise BuilderError.new("You can't use .generated with .soft.")
  end

  decl.generated = true

  self
end

#implementationObject



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/types/private/methods/decl_builder.rb', line 204

def implementation
  check_live!

  case decl.mode
  when Modes.abstract, Modes.override
    raise BuilderError.new("`.implementation` cannot be combined with `.#{decl.mode}`")
  when Modes.standard
    decl.mode = Modes.implementation
  when Modes.overridable
    decl.mode = Modes.overridable_implementation
  when Modes.implementation, Modes.overridable_implementation
    raise BuilderError.new(".implementation cannot be repeated in a single signature")
  end

  self
end

#overridableObject



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/types/private/methods/decl_builder.rb', line 187

def overridable
  check_live!

  case decl.mode
  when Modes.abstract, Modes.override
    raise BuilderError.new("`.overridable` cannot be combined with `.#{decl.mode}`")
  when Modes.standard
    decl.mode = Modes.overridable
  when Modes.implementation
    decl.mode = Modes.overridable_implementation
  when Modes.overridable, Modes.overridable_implementation
    raise BuilderError.new(".overridable cannot be repeated in a single signature")
  end

  self
end

#override(allow_incompatible: false) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/types/private/methods/decl_builder.rb', line 170

def override(allow_incompatible: false)
  check_live!

  case decl.mode
  when Modes.standard
    decl.mode = Modes.override
    decl.override_allow_incompatible = allow_incompatible
  when Modes.override
    raise BuilderError.new(".override cannot be repeated in a single signature")
  else
    raise BuilderError.new("`.override` cannot be combined with any of `.abstract`, `.implementation`, or "\
          "`.overridable`.")
  end

  self
end

#params(params) ⇒ Object



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

def params(params)
  check_live!
  if !decl.params.equal?(ARG_NOT_PROVIDED)
    raise BuilderError.new("You can't call .params twice")
  end

  decl.params = params

  self
end

#returns(type) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/types/private/methods/decl_builder.rb', line 46

def returns(type)
  check_live!
  if decl.returns.is_a?(T::Private::Types::Void)
    raise BuilderError.new("You can't call .returns after calling .void.")
  end
  if !decl.returns.equal?(ARG_NOT_PROVIDED)
    raise BuilderError.new("You can't call .returns multiple times in a signature.")
  end

  decl.returns = type

  self
end

#soft(notify:) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/types/private/methods/decl_builder.rb', line 106

def soft(notify:)
  if T.unsafe(true)
    raise "The .soft API is unstable, so we don't want it used until we redesign it. To change Sorbet's runtime behavior, see https://sorbet.org/docs/tconfiguration"
  end
  check_live!

  if !decl.soft_notify.equal?(ARG_NOT_PROVIDED)
    raise BuilderError.new("You can't call .soft multiple times in a signature.")
  end
  if !decl.checked.equal?(ARG_NOT_PROVIDED)
    raise BuilderError.new("You can't use .soft with .checked.")
  end
  if !decl.generated.equal?(ARG_NOT_PROVIDED)
    raise BuilderError.new("You can't use .soft with .generated.")
  end

  # TODO consider validating that :notify is a project that sentry knows about,
  # as per https://git.corp.stripe.com/stripe-internal/pay-server/blob/master/lib/event/job/sentry_job.rb#L125
  if !notify || notify == ''
    raise BuilderError.new("You can't provide an empty notify to .soft().")
  end

  decl.soft_notify = notify

  self
end

#type_parameters(*names) ⇒ Object

Declares valid type paramaters which can be used with ‘T.type_parameter` in this `sig`.

This is used for generic methods. Example usage:

sig do
  type_parameters(:U)
  .params(blk: T.proc.params(arg0: Elem).returns(T.type_parameter(:U)))
  .returns(T::Array[T.type_parameter(:U)])
end
def map(&blk); end


232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/types/private/methods/decl_builder.rb', line 232

def type_parameters(*names)
  check_live!

  names.each do |name|
    raise BuilderError.new("not a symbol: #{name}") unless name.is_a?(Symbol)
  end

  if !decl.type_parameters.equal?(ARG_NOT_PROVIDED)
    raise BuilderError.new("You can't call .type_parameters multiple times in a signature.")
  end

  decl.type_parameters = names

  self
end

#voidObject



60
61
62
63
64
65
66
67
68
69
# File 'lib/types/private/methods/decl_builder.rb', line 60

def void
  check_live!
  if !decl.returns.equal?(ARG_NOT_PROVIDED)
    raise BuilderError.new("You can't call .void after calling .returns.")
  end

  decl.returns = T::Private::Types::Void.new

  self
end