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

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

Defined Under Namespace

Classes: BuilderError

Constant Summary collapse

FROZEN_HASH =
{}.freeze
FROZEN_ARRAY =
[].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mod, abstract, override, overridable) ⇒ DeclBuilder

Returns a new instance of DeclBuilder.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/types/private/methods/decl_builder.rb', line 18

def initialize(mod, abstract, override, overridable)
  @decl = Declaration.new(
    mod,
    ARG_NOT_PROVIDED, # params
    ARG_NOT_PROVIDED, # returns
    ARG_NOT_PROVIDED, # bind
    Modes.standard, # mode
    nil, # checked
    false, # finalized
    ARG_NOT_PROVIDED, # on_failure
    false, # override_allow_incompatible
    ARG_NOT_PROVIDED, # type_parameters
  )

  # Call the methods after the fact (instead of setting them in the constructor)
  # so we get the BuilderError's, if applicable

  if abstract
    self.abstract
  end

  if override
    self.override(**override)
  end

  if overridable
    self.overridable
  end
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



150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/types/private/methods/decl_builder.rb', line 150

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 `.override` or `.overridable`.")
  end

  self
end

#bind(type) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/types/private/methods/decl_builder.rb', line 101

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



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/types/private/methods/decl_builder.rb', line 112

def checked(level)
  check_live!

  if !decl.checked.nil?
    raise BuilderError.new("You can't call .checked multiple times in a signature.")
  end
  if :never == level && !decl.on_failure.equal?(ARG_NOT_PROVIDED)
    raise BuilderError.new("You can't use .checked(:#{level}) with .on_failure because .on_failure will have no effect.")
  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

#finalObject

Raises:



165
166
167
168
# File 'lib/types/private/methods/decl_builder.rb', line 165

def final
  check_live!
  raise BuilderError.new("The syntax for declaring a method final is `sig(:final) {...}`, not `sig {final. ...}`")
end

#finalize!Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/types/private/methods/decl_builder.rb', line 237

def finalize!
  check_live!

  if decl.returns.equal?(ARG_NOT_PROVIDED)
    raise BuilderError.new("You must provide a return type; use the `.returns` or `.void` builder methods.")
  end

  if decl.bind.equal?(ARG_NOT_PROVIDED)
    decl.bind = nil
  end
  if decl.on_failure.equal?(ARG_NOT_PROVIDED)
    decl.on_failure = nil
  end
  if decl.params.equal?(ARG_NOT_PROVIDED)
    decl.params = FROZEN_HASH
  end
  if decl.type_parameters.equal?(ARG_NOT_PROVIDED)
    decl.type_parameters = FROZEN_ARRAY
  end

  decl.finalized = true

  self
end

#on_failure(*args) ⇒ Object



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

def on_failure(*args)
  check_live!

  if !decl.on_failure.equal?(ARG_NOT_PROVIDED)
    raise BuilderError.new("You can't call .on_failure multiple times in a signature.")
  end
  effective_checked = decl.checked.nil? ? T::Private::RuntimeLevels.default_checked_level : decl.checked
  if effective_checked == :never
    if decl.checked.nil?
      raise BuilderError.new("To use .on_failure you must additionally call .checked(:tests) or .checked(:always), otherwise, the .on_failure has no effect.")
    else
      raise BuilderError.new("You can't use .on_failure with .checked(:#{effective_checked}) because .on_failure will have no effect.")
    end
  end

  decl.on_failure = args

  self
end

#overridableObject



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/types/private/methods/decl_builder.rb', line 193

def overridable
  check_live!

  case decl.mode
  when Modes.abstract
    raise BuilderError.new("`.overridable` cannot be combined with `.#{decl.mode}`")
  when Modes.override
    decl.mode = Modes.overridable_override
  when Modes.standard
    decl.mode = Modes.overridable
  when Modes.overridable, Modes.overridable_override
    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
186
187
188
189
190
191
# 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
    case allow_incompatible
    when true, false, :visibility
      decl.override_allow_incompatible = allow_incompatible
    else
      raise BuilderError.new(".override(allow_incompatible: ...) only accepts `true`, `false`, or `:visibility`, got: #{allow_incompatible.inspect}")
    end
  when Modes.override, Modes.overridable_override
    raise BuilderError.new(".override cannot be repeated in a single signature")
  when Modes.overridable
    decl.mode = Modes.overridable_override
  else
    raise BuilderError.new("`.override` cannot be combined with `.abstract`.")
  end

  self
end

#params(*unused_positional_params, **params) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/types/private/methods/decl_builder.rb', line 48

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

  if unused_positional_params.any?
    some_or_only = params.any? ? "some" : "only"
    raise BuilderError.new(<<~MSG)
      'params' was called with #{some_or_only} positional arguments, but it needs to be called with keyword arguments.
      The keyword arguments' keys must match the name and order of the method's parameters.
    MSG
  end

  if params.empty?
    raise BuilderError.new(<<~MSG)
      'params' was called without any arguments, but it needs to be called with keyword arguments.
      The keyword arguments' keys must match the name and order of the method's parameters.

      Omit 'params' entirely for methods with no parameters.
    MSG
  end

  decl.params = params

  self
end

#returns(type) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/types/private/methods/decl_builder.rb', line 76

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

#type_parameters(*names) ⇒ Object

Declares valid type parameters 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


221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/types/private/methods/decl_builder.rb', line 221

def type_parameters(*names)
  check_live!

  names.each do |name|
    raise BuilderError.new("not a symbol: #{name}") unless Symbol === name
  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



90
91
92
93
94
95
96
97
98
99
# File 'lib/types/private/methods/decl_builder.rb', line 90

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::Private::INSTANCE

  self
end