sorbet-rails 0.6.5.1 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f6f3cc66ce41d3adeddca63531a8d5f6dd98ccc8f936a41d89215ed33423bc2d
4
- data.tar.gz: 9ca5aa9adc679b3e230f3a2fa0c1116bd679a42af9df53821ccbbace6ad6db67
3
+ metadata.gz: 573cda715fb4ceb9881c61dce1f346499a60202ac181548727d7519f8f1b24fe
4
+ data.tar.gz: ba219d4d0ec79076e4a93cf2b6c0eb40272489fb4b71d42eea881c6c7c124b6e
5
5
  SHA512:
6
- metadata.gz: 012505f4ca8d8eed70dba2309dcdecba0fcd76d8695e55e5301fe9c16c033ffccb330acc631a5f38a867f4b8b7dc73e665bad40d3bca767096b741b10c745d95
7
- data.tar.gz: c8b3e2eb74c5bc1f44cc3130deb36c693c4b24aeed08698d4c36da28794f12b228c5b855030106e3b4d41f6d79bb71f38c90c674561551621599051629186931
6
+ metadata.gz: e6d81c40b7c86667834b5063637e9ed29204fda6494aaeccd59781ae481849d7e90b4f5edf678b6727197e6f194c3b06bc87c276e7801013343f2e698c904e3e
7
+ data.tar.gz: 11b034a0b6691ae3d9baf8a2c285b3277c99aa25d280eba8bf12bc78b5a443a4d5d502e814edf1eb576c1bbc108301cfcc0b240cff354a03163497e109ca403c
@@ -0,0 +1,7 @@
1
+ # typed: strong
2
+ module ActiveRecord::Enum
3
+
4
+ sig { params(definitions: T::Hash[Symbol, T.untyped]).void }
5
+ def typed_enum(definitions); end
6
+
7
+ end
@@ -4,8 +4,6 @@ module SorbetRails
4
4
  require 'sorbet-rails/railtie'
5
5
  require 'sorbet-rails/model_rbi_formatter'
6
6
  require 'sorbet-rails/type_assert/type_assert'
7
- require 'sorbet-rails/custom_types/integer_string'
8
- require 'sorbet-rails/custom_types/boolean_string'
9
7
  require 'sorbet-rails/typed_params'
10
8
  end
11
9
  end
@@ -11,6 +11,5 @@
11
11
  # + include GeneratedUrlHelpers
12
12
  # end
13
13
  #
14
- module GeneratedUrlHelpers
15
- include Rails.application.routes.url_helpers
16
- end
14
+ # Reference: https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/routing/route_set.rb
15
+ GeneratedUrlHelpers = Rails.application.routes.url_helpers
@@ -50,9 +50,7 @@ class SorbetRails::Railtie < Rails::Railtie
50
50
  end
51
51
 
52
52
  ActiveSupport.on_load(:action_controller) do
53
- require "sorbet-rails/rails_mixins/custom_params_methods"
54
53
  require "sorbet-rails/rails_mixins/generated_url_helpers"
55
- ActionController::Parameters.include SorbetRails::CustomParamsMethods
56
54
  end
57
55
 
58
56
  SorbetRails.register_configured_plugins
@@ -35,12 +35,12 @@ namespace :rails_rbi do
35
35
  File.write(file_path, inspector.format(SorbetRails::RoutesRbiFormatter.new))
36
36
  end
37
37
 
38
- desc "Copy custom rbis for ActionController::Parameters, pluck_to_struct, etc."
38
+ desc "Copy custom rbis for typed_params, pluck_to_struct, etc."
39
39
  task custom: :environment do
40
40
  copy_bundled_rbi('type_assert.rbi')
41
- copy_bundled_rbi('parameters.rbi')
42
41
  copy_bundled_rbi('pluck_to_tstruct.rbi')
43
42
  copy_bundled_rbi('typed_params.rbi')
43
+ copy_bundled_rbi('typed_enum.rbi')
44
44
 
45
45
  # These files were previously bundled_rbi but are now generated so this
46
46
  # is needed for backwards compatibility with anyone using `rails_rbi:custom`
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = %q{sorbet-rails}
3
- s.version = "0.6.5.1"
3
+ s.version = "0.7.0"
4
4
  s.date = %q{2019-04-18}
5
5
  s.summary = %q{Set of tools to make Sorbet work with Rails seamlessly.}
6
6
  s.authors = ["Chan Zuckerberg Initiative"]
@@ -361,44 +361,6 @@ params = ActionController::Parameters.new({
361
361
  typed_params = TypedParams[MyActionParams].new.extract!(params)
362
362
  T.assert_type!(typed_params, MyActionParams)
363
363
 
364
- # -- require_typed
365
- T.assert_type!(
366
- params.require_typed(:age, TA[Integer].new),
367
- Integer,
368
- )
369
- T.assert_type!(
370
- params.require_typed(:name, TA[String].new),
371
- String,
372
- )
373
- info = params.require_typed(:info, TA[ActionController::Parameters].new)
374
- T.assert_type!(info, ActionController::Parameters)
375
- T.assert_type!(
376
- info.require_typed(:friends, TA[T::Array[String]].new),
377
- T::Array[String],
378
- )
379
- # -- fetch_typed
380
- T.assert_type!(
381
- params.fetch_typed(:age, TA[Integer].new),
382
- Integer,
383
- )
384
- T.assert_type!(
385
- params.fetch_typed(:name, TA[String].new),
386
- String,
387
- )
388
- T.assert_type!(
389
- params.fetch_typed(:nonexistence, TA[String].new, ''),
390
- String,
391
- )
392
- T.assert_type!(
393
- params.fetch_typed(:nonexistence, TA[T.nilable(String)].new, nil),
394
- T.nilable(String),
395
- )
396
- T.assert_type!(
397
- params.fetch_typed(:nonexistence, TA[T::Array[Integer]].new, []),
398
- T::Array[Integer],
399
- )
400
-
401
-
402
364
  # -- pluck to tstruct
403
365
  class WizardStruct < T::Struct
404
366
  const :name, String
@@ -415,23 +377,16 @@ Wizard.all.pluck_to_tstruct(TA[WizardStruct].new).each do |row|
415
377
  T.assert_type!(row, WizardStruct)
416
378
  end
417
379
 
418
-
419
380
  # -- GeneratedUrlHelpers
420
381
  class TestHelper
421
382
  include GeneratedUrlHelpers
422
-
423
- # need to implement this for the url
424
- def default_url_options
425
- {
426
- protocol: 'http',
427
- host: 'localhost',
428
- port: 3000,
429
- }
430
- end
431
-
432
- def test_url_helper
433
- T.assert_type!(test_index_path, String)
434
- T.assert_type!(test_index_url, String)
435
- end
436
383
  end
437
- TestHelper.new.test_url_helper
384
+
385
+ T.assert_type!(TestHelper.new.test_index_path, String)
386
+ # need to set this config for _url methods
387
+ Rails.application.routes.default_url_options = {
388
+ protocol: 'http',
389
+ host: 'localhost',
390
+ port: 3000,
391
+ }
392
+ T.assert_type!(TestHelper.new.test_index_url, String)
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../../..
3
3
  specs:
4
- sorbet-rails (0.6.5.1)
4
+ sorbet-rails (0.7.0)
5
5
  method_source (>= 0.9.2)
6
6
  parlour (~> 2.0)
7
7
  parser (>= 2.7)
@@ -361,44 +361,6 @@ params = ActionController::Parameters.new({
361
361
  typed_params = TypedParams[MyActionParams].new.extract!(params)
362
362
  T.assert_type!(typed_params, MyActionParams)
363
363
 
364
- # -- require_typed
365
- T.assert_type!(
366
- params.require_typed(:age, TA[Integer].new),
367
- Integer,
368
- )
369
- T.assert_type!(
370
- params.require_typed(:name, TA[String].new),
371
- String,
372
- )
373
- info = params.require_typed(:info, TA[ActionController::Parameters].new)
374
- T.assert_type!(info, ActionController::Parameters)
375
- T.assert_type!(
376
- info.require_typed(:friends, TA[T::Array[String]].new),
377
- T::Array[String],
378
- )
379
- # -- fetch_typed
380
- T.assert_type!(
381
- params.fetch_typed(:age, TA[Integer].new),
382
- Integer,
383
- )
384
- T.assert_type!(
385
- params.fetch_typed(:name, TA[String].new),
386
- String,
387
- )
388
- T.assert_type!(
389
- params.fetch_typed(:nonexistence, TA[String].new, ''),
390
- String,
391
- )
392
- T.assert_type!(
393
- params.fetch_typed(:nonexistence, TA[T.nilable(String)].new, nil),
394
- T.nilable(String),
395
- )
396
- T.assert_type!(
397
- params.fetch_typed(:nonexistence, TA[T::Array[Integer]].new, []),
398
- T::Array[Integer],
399
- )
400
-
401
-
402
364
  # -- pluck to tstruct
403
365
  class WizardStruct < T::Struct
404
366
  const :name, String
@@ -415,23 +377,16 @@ Wizard.all.pluck_to_tstruct(TA[WizardStruct].new).each do |row|
415
377
  T.assert_type!(row, WizardStruct)
416
378
  end
417
379
 
418
-
419
380
  # -- GeneratedUrlHelpers
420
381
  class TestHelper
421
382
  include GeneratedUrlHelpers
422
-
423
- # need to implement this for the url
424
- def default_url_options
425
- {
426
- protocol: 'http',
427
- host: 'localhost',
428
- port: 3000,
429
- }
430
- end
431
-
432
- def test_url_helper
433
- T.assert_type!(test_index_path, String)
434
- T.assert_type!(test_index_url, String)
435
- end
436
383
  end
437
- TestHelper.new.test_url_helper
384
+
385
+ T.assert_type!(TestHelper.new.test_index_path, String)
386
+ # need to set this config for _url methods
387
+ Rails.application.routes.default_url_options = {
388
+ protocol: 'http',
389
+ host: 'localhost',
390
+ port: 3000,
391
+ }
392
+ T.assert_type!(TestHelper.new.test_index_url, String)
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../../..
3
3
  specs:
4
- sorbet-rails (0.6.5.1)
4
+ sorbet-rails (0.7.0)
5
5
  method_source (>= 0.9.2)
6
6
  parlour (~> 2.0)
7
7
  parser (>= 2.7)
@@ -361,44 +361,6 @@ params = ActionController::Parameters.new({
361
361
  typed_params = TypedParams[MyActionParams].new.extract!(params)
362
362
  T.assert_type!(typed_params, MyActionParams)
363
363
 
364
- # -- require_typed
365
- T.assert_type!(
366
- params.require_typed(:age, TA[Integer].new),
367
- Integer,
368
- )
369
- T.assert_type!(
370
- params.require_typed(:name, TA[String].new),
371
- String,
372
- )
373
- info = params.require_typed(:info, TA[ActionController::Parameters].new)
374
- T.assert_type!(info, ActionController::Parameters)
375
- T.assert_type!(
376
- info.require_typed(:friends, TA[T::Array[String]].new),
377
- T::Array[String],
378
- )
379
- # -- fetch_typed
380
- T.assert_type!(
381
- params.fetch_typed(:age, TA[Integer].new),
382
- Integer,
383
- )
384
- T.assert_type!(
385
- params.fetch_typed(:name, TA[String].new),
386
- String,
387
- )
388
- T.assert_type!(
389
- params.fetch_typed(:nonexistence, TA[String].new, ''),
390
- String,
391
- )
392
- T.assert_type!(
393
- params.fetch_typed(:nonexistence, TA[T.nilable(String)].new, nil),
394
- T.nilable(String),
395
- )
396
- T.assert_type!(
397
- params.fetch_typed(:nonexistence, TA[T::Array[Integer]].new, []),
398
- T::Array[Integer],
399
- )
400
-
401
-
402
364
  # -- pluck to tstruct
403
365
  class WizardStruct < T::Struct
404
366
  const :name, String
@@ -415,23 +377,16 @@ Wizard.all.pluck_to_tstruct(TA[WizardStruct].new).each do |row|
415
377
  T.assert_type!(row, WizardStruct)
416
378
  end
417
379
 
418
-
419
380
  # -- GeneratedUrlHelpers
420
381
  class TestHelper
421
382
  include GeneratedUrlHelpers
422
-
423
- # need to implement this for the url
424
- def default_url_options
425
- {
426
- protocol: 'http',
427
- host: 'localhost',
428
- port: 3000,
429
- }
430
- end
431
-
432
- def test_url_helper
433
- T.assert_type!(test_index_path, String)
434
- T.assert_type!(test_index_url, String)
435
- end
436
383
  end
437
- TestHelper.new.test_url_helper
384
+
385
+ T.assert_type!(TestHelper.new.test_index_path, String)
386
+ # need to set this config for _url methods
387
+ Rails.application.routes.default_url_options = {
388
+ protocol: 'http',
389
+ host: 'localhost',
390
+ port: 3000,
391
+ }
392
+ T.assert_type!(TestHelper.new.test_index_url, String)
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../../..
3
3
  specs:
4
- sorbet-rails (0.6.5.1)
4
+ sorbet-rails (0.7.0)
5
5
  method_source (>= 0.9.2)
6
6
  parlour (~> 2.0)
7
7
  parser (>= 2.7)
@@ -361,44 +361,6 @@ params = ActionController::Parameters.new({
361
361
  typed_params = TypedParams[MyActionParams].new.extract!(params)
362
362
  T.assert_type!(typed_params, MyActionParams)
363
363
 
364
- # -- require_typed
365
- T.assert_type!(
366
- params.require_typed(:age, TA[Integer].new),
367
- Integer,
368
- )
369
- T.assert_type!(
370
- params.require_typed(:name, TA[String].new),
371
- String,
372
- )
373
- info = params.require_typed(:info, TA[ActionController::Parameters].new)
374
- T.assert_type!(info, ActionController::Parameters)
375
- T.assert_type!(
376
- info.require_typed(:friends, TA[T::Array[String]].new),
377
- T::Array[String],
378
- )
379
- # -- fetch_typed
380
- T.assert_type!(
381
- params.fetch_typed(:age, TA[Integer].new),
382
- Integer,
383
- )
384
- T.assert_type!(
385
- params.fetch_typed(:name, TA[String].new),
386
- String,
387
- )
388
- T.assert_type!(
389
- params.fetch_typed(:nonexistence, TA[String].new, ''),
390
- String,
391
- )
392
- T.assert_type!(
393
- params.fetch_typed(:nonexistence, TA[T.nilable(String)].new, nil),
394
- T.nilable(String),
395
- )
396
- T.assert_type!(
397
- params.fetch_typed(:nonexistence, TA[T::Array[Integer]].new, []),
398
- T::Array[Integer],
399
- )
400
-
401
-
402
364
  # -- pluck to tstruct
403
365
  class WizardStruct < T::Struct
404
366
  const :name, String
@@ -415,23 +377,16 @@ Wizard.all.pluck_to_tstruct(TA[WizardStruct].new).each do |row|
415
377
  T.assert_type!(row, WizardStruct)
416
378
  end
417
379
 
418
-
419
380
  # -- GeneratedUrlHelpers
420
381
  class TestHelper
421
382
  include GeneratedUrlHelpers
422
-
423
- # need to implement this for the url
424
- def default_url_options
425
- {
426
- protocol: 'http',
427
- host: 'localhost',
428
- port: 3000,
429
- }
430
- end
431
-
432
- def test_url_helper
433
- T.assert_type!(test_index_path, String)
434
- T.assert_type!(test_index_url, String)
435
- end
436
383
  end
437
- TestHelper.new.test_url_helper
384
+
385
+ T.assert_type!(TestHelper.new.test_index_path, String)
386
+ # need to set this config for _url methods
387
+ Rails.application.routes.default_url_options = {
388
+ protocol: 'http',
389
+ host: 'localhost',
390
+ port: 3000,
391
+ }
392
+ T.assert_type!(TestHelper.new.test_index_url, String)
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../../..
3
3
  specs:
4
- sorbet-rails (0.6.5.1)
4
+ sorbet-rails (0.7.0)
5
5
  method_source (>= 0.9.2)
6
6
  parlour (~> 2.0)
7
7
  parser (>= 2.7)
@@ -361,44 +361,6 @@ params = ActionController::Parameters.new({
361
361
  typed_params = TypedParams[MyActionParams].new.extract!(params)
362
362
  T.assert_type!(typed_params, MyActionParams)
363
363
 
364
- # -- require_typed
365
- T.assert_type!(
366
- params.require_typed(:age, TA[Integer].new),
367
- Integer,
368
- )
369
- T.assert_type!(
370
- params.require_typed(:name, TA[String].new),
371
- String,
372
- )
373
- info = params.require_typed(:info, TA[ActionController::Parameters].new)
374
- T.assert_type!(info, ActionController::Parameters)
375
- T.assert_type!(
376
- info.require_typed(:friends, TA[T::Array[String]].new),
377
- T::Array[String],
378
- )
379
- # -- fetch_typed
380
- T.assert_type!(
381
- params.fetch_typed(:age, TA[Integer].new),
382
- Integer,
383
- )
384
- T.assert_type!(
385
- params.fetch_typed(:name, TA[String].new),
386
- String,
387
- )
388
- T.assert_type!(
389
- params.fetch_typed(:nonexistence, TA[String].new, ''),
390
- String,
391
- )
392
- T.assert_type!(
393
- params.fetch_typed(:nonexistence, TA[T.nilable(String)].new, nil),
394
- T.nilable(String),
395
- )
396
- T.assert_type!(
397
- params.fetch_typed(:nonexistence, TA[T::Array[Integer]].new, []),
398
- T::Array[Integer],
399
- )
400
-
401
-
402
364
  # -- pluck to tstruct
403
365
  class WizardStruct < T::Struct
404
366
  const :name, String
@@ -415,23 +377,16 @@ Wizard.all.pluck_to_tstruct(TA[WizardStruct].new).each do |row|
415
377
  T.assert_type!(row, WizardStruct)
416
378
  end
417
379
 
418
-
419
380
  # -- GeneratedUrlHelpers
420
381
  class TestHelper
421
382
  include GeneratedUrlHelpers
422
-
423
- # need to implement this for the url
424
- def default_url_options
425
- {
426
- protocol: 'http',
427
- host: 'localhost',
428
- port: 3000,
429
- }
430
- end
431
-
432
- def test_url_helper
433
- T.assert_type!(test_index_path, String)
434
- T.assert_type!(test_index_url, String)
435
- end
436
383
  end
437
- TestHelper.new.test_url_helper
384
+
385
+ T.assert_type!(TestHelper.new.test_index_path, String)
386
+ # need to set this config for _url methods
387
+ Rails.application.routes.default_url_options = {
388
+ protocol: 'http',
389
+ host: 'localhost',
390
+ port: 3000,
391
+ }
392
+ T.assert_type!(TestHelper.new.test_index_url, String)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sorbet-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.5.1
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chan Zuckerberg Initiative
@@ -200,15 +200,13 @@ files:
200
200
  - Rakefile
201
201
  - SECURITY.md
202
202
  - bin/sorbet
203
- - lib/bundled_rbi/parameters.rbi
204
203
  - lib/bundled_rbi/pluck_to_tstruct.rbi
205
204
  - lib/bundled_rbi/type_assert.rbi
205
+ - lib/bundled_rbi/typed_enum.rbi
206
206
  - lib/bundled_rbi/typed_params.rbi
207
207
  - lib/sorbet-rails.rb
208
208
  - lib/sorbet-rails/active_record_rbi_formatter.rb
209
209
  - lib/sorbet-rails/config.rb
210
- - lib/sorbet-rails/custom_types/boolean_string.rb
211
- - lib/sorbet-rails/custom_types/integer_string.rb
212
210
  - lib/sorbet-rails/dependent_gem_rbis/README.md
213
211
  - lib/sorbet-rails/dependent_gem_rbis/actionmailer.rbi
214
212
  - lib/sorbet-rails/dependent_gem_rbis/activejob.rbi
@@ -239,7 +237,6 @@ files:
239
237
  - lib/sorbet-rails/model_utils.rb
240
238
  - lib/sorbet-rails/rails_mixins/active_record_overrides.rb
241
239
  - lib/sorbet-rails/rails_mixins/custom_finder_methods.rb
242
- - lib/sorbet-rails/rails_mixins/custom_params_methods.rb
243
240
  - lib/sorbet-rails/rails_mixins/generated_url_helpers.rb
244
241
  - lib/sorbet-rails/rails_mixins/pluck_to_tstruct.rb
245
242
  - lib/sorbet-rails/railtie.rb
@@ -257,13 +254,10 @@ files:
257
254
  - spec/bin/run_all_specs.sh
258
255
  - spec/bin/run_spec.sh
259
256
  - spec/bin/update_test_data.sh
260
- - spec/boolean_string_spec.rb
261
257
  - spec/custom_finder_methods_spec.rb
262
- - spec/custom_params_methods_spec.rb
263
258
  - spec/generators/rails-template.rb
264
259
  - spec/generators/sorbet_test_cases.rb
265
260
  - spec/helper_rbi_formatter_spec.rb
266
- - spec/integer_string_spec.rb
267
261
  - spec/job_rbi_formatter_spec.rb
268
262
  - spec/mailer_rbi_formatter_spec.rb
269
263
  - spec/model_plugins_spec.rb
@@ -788,13 +782,10 @@ test_files:
788
782
  - spec/bin/run_all_specs.sh
789
783
  - spec/bin/run_spec.sh
790
784
  - spec/bin/update_test_data.sh
791
- - spec/boolean_string_spec.rb
792
785
  - spec/custom_finder_methods_spec.rb
793
- - spec/custom_params_methods_spec.rb
794
786
  - spec/generators/rails-template.rb
795
787
  - spec/generators/sorbet_test_cases.rb
796
788
  - spec/helper_rbi_formatter_spec.rb
797
- - spec/integer_string_spec.rb
798
789
  - spec/job_rbi_formatter_spec.rb
799
790
  - spec/mailer_rbi_formatter_spec.rb
800
791
  - spec/model_plugins_spec.rb
@@ -1,28 +0,0 @@
1
- # typed: strong
2
-
3
- module SorbetRails::CustomParamsMethods
4
- extend T::Sig
5
-
6
- sig {
7
- type_parameters(:U).
8
- params(key: Symbol, ta: ITypeAssert[T.type_parameter(:U)]).
9
- returns(T.type_parameter(:U))
10
- }
11
- def require_typed(key, ta); end
12
-
13
- # Note: when default value is a hash, it'll be converted into an ActionController::Parameters
14
- sig {
15
- type_parameters(:U).
16
- params(
17
- key: Symbol,
18
- ta: ITypeAssert[T.type_parameter(:U)],
19
- args: T.untyped,
20
- ).
21
- returns(T.type_parameter(:U))
22
- }
23
- def fetch_typed(key, ta, *args); end
24
- end
25
-
26
- class ActionController::Parameters
27
- include SorbetRails::CustomParamsMethods
28
- end
@@ -1,42 +0,0 @@
1
- # typed: false
2
- require('sorbet-rails/deprecation.rb')
3
-
4
- module BooleanStringImpl
5
- def is_a?(type)
6
- return super unless type == BooleanString
7
- _is_a_boolean_string?
8
- end
9
-
10
- def kind_of?(type)
11
- return super unless type == BooleanString
12
- _is_a_boolean_string?
13
- end
14
-
15
- def instance_of?(type)
16
- return super unless type == BooleanString
17
- _is_a_boolean_string?
18
- end
19
-
20
- def _is_a_boolean_string?
21
- return @cached_is_a unless @cached_is_a.nil?
22
- SorbetRails::TypeAssertDeprecation.deprecation_warning(
23
- :BooleanString,
24
- 'Use TypedParam with T::Boolean type instead.'
25
- )
26
- @cached_is_a = (self =~ /^(true|false)$/i) == 0
27
- end
28
- end
29
-
30
- class String
31
- include BooleanStringImpl
32
- end
33
-
34
- class BooleanString < String
35
- def self.===(other)
36
- SorbetRails::TypeAssertDeprecation.deprecation_warning(
37
- :BooleanString,
38
- 'Use TypedParam with T::Boolean type instead.'
39
- )
40
- other.is_a?(BooleanString)
41
- end
42
- end
@@ -1,45 +0,0 @@
1
- # typed: false
2
- require('sorbet-rails/deprecation.rb')
3
-
4
- module IntegerStringImpl
5
- def is_a?(type)
6
- return super unless type == IntegerString
7
- _is_a_integer_string?
8
- end
9
-
10
- def kind_of?(type)
11
- return super unless type == IntegerString
12
- _is_a_integer_string?
13
- end
14
-
15
- def instance_of?(type)
16
- return super unless type == IntegerString
17
- _is_a_integer_string?
18
- end
19
-
20
- def _is_a_integer_string?
21
- return @cached_is_a unless @cached_is_a.nil?
22
- SorbetRails::TypeAssertDeprecation.deprecation_warning(
23
- :IntegerString,
24
- 'Use TypedParams with Integer type instead.'
25
- )
26
- Integer(self, 10)
27
- @cached_is_a = true
28
- rescue ArgumentError => err
29
- @cached_is_a = false
30
- end
31
- end
32
-
33
- class String
34
- include IntegerStringImpl
35
- end
36
-
37
- class IntegerString < String
38
- def self.===(other)
39
- SorbetRails::TypeAssertDeprecation.deprecation_warning(
40
- :IntegerString,
41
- 'Use TypedParams with Integer type instead.'
42
- )
43
- other.is_a?(IntegerString)
44
- end
45
- end
@@ -1,57 +0,0 @@
1
- # typed: false
2
- require 'sorbet-runtime'
3
- require('sorbet-rails/deprecation.rb')
4
-
5
- module SorbetRails::CustomParamsMethods
6
- include Kernel
7
- extend T::Sig
8
- extend T::Helpers
9
-
10
- # methods exists on ActionController::Parameter
11
- # def require(key); super; end
12
- # def fetch(key, *args); super; end
13
-
14
- sig {
15
- type_parameters(:U).
16
- params(key: Symbol, ta: ITypeAssert[T.type_parameter(:U)]).
17
- returns(T.type_parameter(:U))
18
- }
19
- def require_typed(key, ta)
20
- SorbetRails::TypeAssertDeprecation.deprecation_warning(
21
- :require_typed,
22
- 'Use TypedParams with a T::Struct represents the type of each parameters.'
23
- )
24
-
25
- val = require(key)
26
- ta.assert(val)
27
- rescue TypeError
28
- raise ActionController::BadRequest.new(
29
- "Expected parameter #{key} to be an instance of type #{ta.get_type}, got `#{val}`"
30
- )
31
- end
32
-
33
- # Note: when default value is a hash, it'll be converted into an ActionController::Parameters
34
- sig {
35
- type_parameters(:U).
36
- params(
37
- key: Symbol,
38
- ta: ITypeAssert[T.type_parameter(:U)],
39
- args: T.untyped,
40
- ).
41
- returns(T.type_parameter(:U))
42
- }
43
- def fetch_typed(key, ta, *args)
44
- SorbetRails::TypeAssertDeprecation.deprecation_warning(
45
- :fetch_typed,
46
- 'Use TypedParams with a T::Struct represents the type of each parameters.'
47
- )
48
-
49
- val = fetch(key, *args)
50
- ta.assert(val)
51
- rescue TypeError
52
- raise ActionController::BadRequest.new(
53
- "Expected parameter #{key} to be an instance of type T.nilable(#{ta.get_type}),
54
- got `#{val}`".squish!
55
- )
56
- end
57
- end
@@ -1,59 +0,0 @@
1
- require 'rails_helper'
2
- require 'sorbet-rails/custom_types/boolean_string'
3
- require 'sorbet-runtime'
4
-
5
- RSpec.describe BooleanString do
6
- shared_examples 'boolean string' do |str|
7
- it 'acts as BooleanString perfectly' do
8
- expect(str.is_a?(BooleanString)).to be(true)
9
- expect(str.kind_of?(BooleanString)).to be(true)
10
- expect(str.instance_of?(BooleanString)).to be(true)
11
- expect(BooleanString === str).to be(true)
12
- end
13
- end
14
-
15
- include_examples 'boolean string', 'true'
16
- include_examples 'boolean string', 'false'
17
- include_examples 'boolean string', 'True'
18
- include_examples 'boolean string', 'False'
19
-
20
- shared_examples 'non boolean string' do |str|
21
- it 'does not acts as BooleanString' do
22
- expect(str.is_a?(BooleanString)).to be(false)
23
- expect(str.kind_of?(BooleanString)).to be(false)
24
- expect(str.instance_of?(BooleanString)).to be(false)
25
- expect(BooleanString === str).to be(false)
26
- end
27
- end
28
-
29
- include_examples 'non boolean string', 'yes'
30
- include_examples 'non boolean string', 'no'
31
- include_examples 'non boolean string', 'alala'
32
-
33
- context 'sorbet recognizes it at runtime' do
34
- it 'lets boolean string pass runtime check' do
35
- expect(T.let('true', BooleanString)).to eql('true')
36
- end
37
-
38
- it 'doesnt let normal string pass runtime typecheck' do
39
- expect {
40
- T.let('yes', BooleanString)
41
- }.to raise_error(TypeError)
42
- end
43
- end
44
-
45
- context 'using with TypeAssert' do
46
- let!(:ta) { TA[BooleanString].new }
47
-
48
- it 'lets boolean string pass runtime typecheck' do
49
- expect(ta.assert('true')).to eql('true')
50
- end
51
-
52
- it 'doesnt let normal string pass runtime typecheck' do
53
- expect {
54
- ta.assert('yes')
55
- }.to raise_error(TypeError)
56
- end
57
- end
58
- end
59
-
@@ -1,138 +0,0 @@
1
- require 'rails_helper'
2
-
3
- describe ActionController::Parameters do
4
- let!(:params) do
5
- ActionController::Parameters.new({
6
- age: 11,
7
- name: 'Harry Potter',
8
- info: {
9
- birthday: Date.parse('1980-07-31'),
10
- friends: [
11
- 'Hermione',
12
- 'Ron',
13
- ],
14
- grandson: nil,
15
- },
16
- })
17
- end
18
- let!(:params_info) { params.require(:info) }
19
-
20
- context 'require_typed' do
21
- it 'gets basic param correctly' do
22
- val = params.require_typed(:age, TA[Integer].new)
23
- expect(val).to eql(11)
24
-
25
- val = params.require_typed(:name, TA[String].new)
26
- expect(val).to eql('Harry Potter')
27
- end
28
-
29
- it 'gets param with complex type correctly' do
30
- val = params.require_typed(:age, TA[T.any(String, Integer)].new)
31
- expect(val).to eql(11)
32
- end
33
-
34
- it 'gets param object correctly' do
35
- val = params.require_typed(:info, TA[ActionController::Parameters].new)
36
- expect(val.is_a?(ActionController::Parameters))
37
- expect(val).to eql(params_info)
38
-
39
- friends = val.require_typed(:friends, TA[T::Array[String]].new)
40
- expect(friends).to eql([
41
- 'Hermione',
42
- 'Ron',
43
- ])
44
- end
45
-
46
- it 'raises error when param doesnt exist' do
47
- expect {
48
- params.require_typed(:nonexistence, TA[String].new)
49
- }.to raise_error(ActionController::ParameterMissing)
50
- end
51
-
52
- it 'raises error when param is nil' do
53
- expect {
54
- params_info.require_typed(:grandson, TA[T::Array[String]].new)
55
- }.to raise_error(ActionController::ParameterMissing)
56
- end
57
-
58
- it 'raises error when param has wrong type' do
59
- expect {
60
- params.require_typed(:age, TA[String].new)
61
- }.to raise_error(ActionController::BadRequest)
62
- end
63
-
64
- it 'raises error when param has wrong complex type' do
65
- expect {
66
- params.require_typed(:age, TA[T::Array[String]].new)
67
- }.to raise_error(ActionController::BadRequest)
68
- end
69
- end
70
-
71
- context 'fetch_typed' do
72
- it 'gets basic param correctly' do
73
- val = params.fetch_typed(:age, TA[Integer].new)
74
- expect(val).to eql(11)
75
-
76
- val = params.fetch_typed(:name, TA[String].new)
77
- expect(val).to eql('Harry Potter')
78
- end
79
-
80
- it 'gets param object correctly' do
81
- val = params.fetch_typed(:info, TA[ActionController::Parameters].new)
82
- expect(val.is_a?(ActionController::Parameters))
83
- expect(val).to eql(params_info)
84
-
85
- friends = val.fetch_typed(:friends, TA[T::Array[String]].new)
86
- expect(friends).to eql([
87
- 'Hermione',
88
- 'Ron',
89
- ])
90
- end
91
-
92
- it 'raises error when param doesnt exist' do
93
- expect {
94
- val = params.fetch_typed(:nonexistence, TA[String].new)
95
- }.to raise_error(ActionController::ParameterMissing)
96
- end
97
-
98
- it 'raises error when param has wrong type' do
99
- expect {
100
- val = params.fetch_typed(:age, TA[String].new)
101
- }.to raise_error(ActionController::BadRequest)
102
- end
103
-
104
- it 'returns nil when param is nil' do
105
- grandson = params_info.fetch_typed(:grandson, TA[T.nilable(T::Array[String])].new)
106
- expect(grandson).to eql(nil)
107
- end
108
-
109
- context 'when there is a default value' do
110
- it 'uses default value when param is nil and has a default' do
111
- fetched = params.fetch_typed(:nonexistence, TA[T::Array[String]].new, [])
112
- expect(fetched).to eql([])
113
- end
114
-
115
- it 'ignores default value when there is a real value' do
116
- name = params.fetch_typed(:name, TA[String].new, 'John Doe')
117
- expect(name).to eql('Harry Potter')
118
- end
119
-
120
- it 'converts hash default value into a params' do
121
- # this is `fetch` documented behavior
122
- fetched = params.fetch_typed(
123
- :nonexistence,
124
- TA[ActionController::Parameters].new,
125
- { unknown: "baby" },
126
- )
127
- expect(fetched.is_a?(ActionController::Parameters)).to be(true)
128
- expect(fetched.permit(:unknown).to_h).to eql({ "unknown" => "baby" })
129
- end
130
-
131
- it 'raises error when param is nil and given a wrong default' do
132
- expect {
133
- fetched = params.fetch_typed(:nonexistence, TA[T::Array].new, 5)
134
- }.to raise_error(ActionController::BadRequest)
135
- end
136
- end
137
- end
138
- end
@@ -1,46 +0,0 @@
1
- require 'rails_helper'
2
- require 'sorbet-rails/custom_types/integer_string'
3
- require 'sorbet-runtime'
4
-
5
- RSpec.describe IntegerString do
6
- it 'lets integer string acts as IntegerString perfectly' do
7
- expect('1'.is_a?(IntegerString)).to be(true)
8
- expect('12'.kind_of?(IntegerString)).to be(true)
9
- expect('123'.instance_of?(IntegerString)).to be(true)
10
- expect(IntegerString === '1234').to be(true)
11
- end
12
-
13
- it 'does not let other string acts as IntegerString' do
14
- expect('a1'.is_a?(IntegerString)).to be(false)
15
- expect('a12'.kind_of?(IntegerString)).to be(false)
16
- expect('a123'.instance_of?(IntegerString)).to be(false)
17
- expect(IntegerString === 'a1234').to be(false)
18
- end
19
-
20
- context 'sorbet recognizes it at runtime' do
21
- it 'lets integer string pass runtime check' do
22
- expect(T.let('123', IntegerString)).to eql('123')
23
- end
24
-
25
- it 'doesnt let normal string pass runtime typecheck' do
26
- expect {
27
- T.let('a123', IntegerString)
28
- }.to raise_error(TypeError)
29
- end
30
- end
31
-
32
- context 'using with TypeAssert' do
33
- let!(:ta) { TA[IntegerString].new }
34
-
35
- it 'lets integer string pass runtime typecheck' do
36
- expect(ta.assert('123')).to eql('123')
37
- end
38
-
39
- it 'doesnt let normal string pass runtime typecheck' do
40
- expect {
41
- ta.assert('a123')
42
- }.to raise_error(TypeError)
43
- end
44
- end
45
- end
46
-