sorbet-runtime 0.6.13414 → 0.6.13416
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 +4 -4
- data/lib/types/props/_props.rb +0 -15
- data/lib/types/props/decorator.rb +1 -135
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e7ce269428fae9739dd14208d14c2b0083cc8a4ad608fbf5f58426b3b210a240
|
|
4
|
+
data.tar.gz: bbc5c347477acd3dae23cee4339118b2af0af6f1c69bb635991c17172da3e111
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 0623e1601d3253a1b19eb58e9f222c915119c1525b87f32dee79ca78ac10bdf00a830e3f21804b8a2ec044892187d6c18ba4447e36a1f8c1b6c4b0c2334c083f
|
|
7
|
+
data.tar.gz: 480964068adcccebf112bec72c256ad499c4c89c809d2b22c12887f764cb50c0c5c6ae54f7a23bb6324b2320642b2a934b4398ce692c2de458a72c441b401414
|
data/lib/types/props/_props.rb
CHANGED
|
@@ -76,21 +76,6 @@ module T::Props
|
|
|
76
76
|
#
|
|
77
77
|
# ``:ifunset`` is considered **DEPRECATED** and should not be used
|
|
78
78
|
# in new code, in favor of just setting a default value.
|
|
79
|
-
# @option rules [Model, Symbol, Proc] :foreign A model class that this
|
|
80
|
-
# property is a reference to. Passing `:foreign` will define a
|
|
81
|
-
# `:"#{name}_"` method, that will load and return the
|
|
82
|
-
# corresponding foreign model.
|
|
83
|
-
#
|
|
84
|
-
# A symbol can be passed to avoid load-order dependencies; It
|
|
85
|
-
# will be lazily resolved relative to the enclosing module of the
|
|
86
|
-
# defining class.
|
|
87
|
-
#
|
|
88
|
-
# A callable (proc or method) can be passed to dynamically specify the
|
|
89
|
-
# foreign model. This will be passed the object instance so that other
|
|
90
|
-
# properties of the object can be used to determine the relevant model
|
|
91
|
-
# class. It should return a string/symbol class name or the foreign model
|
|
92
|
-
# class directly.
|
|
93
|
-
#
|
|
94
79
|
# @option rules [Object] :default A default value that will be set
|
|
95
80
|
# by `#initialize` if none is provided in the initialization
|
|
96
81
|
# hash. This will not affect objects loaded by {.from_hash}.
|
|
@@ -89,7 +89,6 @@ class T::Props::Decorator
|
|
|
89
89
|
# solve the problem you're encountering.
|
|
90
90
|
VALID_RULE_KEYS = T.let(%i[
|
|
91
91
|
enum
|
|
92
|
-
foreign
|
|
93
92
|
ifunset
|
|
94
93
|
immutable
|
|
95
94
|
override
|
|
@@ -211,23 +210,6 @@ class T::Props::Decorator
|
|
|
211
210
|
end
|
|
212
211
|
alias_method :get, :prop_get_if_set # Alias for backwards compatibility
|
|
213
212
|
|
|
214
|
-
# checked(:never) - O(prop accesses)
|
|
215
|
-
sig do
|
|
216
|
-
params(
|
|
217
|
-
instance: DecoratedInstance,
|
|
218
|
-
prop: Symbol,
|
|
219
|
-
foreign_class: T::Module[T.anything],
|
|
220
|
-
rules: Rules,
|
|
221
|
-
opts: T::Hash[Symbol, T.untyped],
|
|
222
|
-
)
|
|
223
|
-
.returns(T.untyped)
|
|
224
|
-
.checked(:never)
|
|
225
|
-
end
|
|
226
|
-
def foreign_prop_get(instance, prop, foreign_class, rules=prop_rules(prop), opts={})
|
|
227
|
-
return if !(value = prop_get(instance, prop, rules))
|
|
228
|
-
T.unsafe(foreign_class).load(value, {}, opts)
|
|
229
|
-
end
|
|
230
|
-
|
|
231
213
|
# TODO: we should really be checking all the methods on `cls`, not just Object
|
|
232
214
|
BANNED_METHOD_NAMES = T.let(Object.instance_methods.each_with_object({}) { |x, acc| acc[x] = true }.freeze, T::Hash[Symbol, TrueClass], checked: false)
|
|
233
215
|
|
|
@@ -297,7 +279,7 @@ class T::Props::Decorator
|
|
|
297
279
|
Set
|
|
298
280
|
when T::Types::Union
|
|
299
281
|
# The below unwraps our T.nilable types for T::Props if we can.
|
|
300
|
-
# This lets us do things like specify
|
|
282
|
+
# This lets us do things like specify `const :foo, T.nilable(String)`
|
|
301
283
|
non_nil_type = T::Utils.unwrap_nilable(type)
|
|
302
284
|
if non_nil_type
|
|
303
285
|
convert_type_to_class(non_nil_type)
|
|
@@ -450,7 +432,6 @@ class T::Props::Decorator
|
|
|
450
432
|
# get at the property (e.g., Chalk::ODM::Document exposes `get` and `set`).
|
|
451
433
|
define_getter_and_setter(name, rules) unless rules[:without_accessors]
|
|
452
434
|
|
|
453
|
-
handle_foreign_option(name, cls, rules, rules[:foreign]) if rules[:foreign]
|
|
454
435
|
handle_redaction_option(name, rules[:redaction]) if rules[:redaction]
|
|
455
436
|
end
|
|
456
437
|
|
|
@@ -525,121 +506,6 @@ class T::Props::Decorator
|
|
|
525
506
|
end
|
|
526
507
|
end
|
|
527
508
|
|
|
528
|
-
sig do
|
|
529
|
-
params(
|
|
530
|
-
option_sym: Symbol,
|
|
531
|
-
foreign: T.untyped,
|
|
532
|
-
valid_type_msg: String,
|
|
533
|
-
)
|
|
534
|
-
.void
|
|
535
|
-
.checked(:never)
|
|
536
|
-
end
|
|
537
|
-
private def validate_foreign_option(option_sym, foreign, valid_type_msg:)
|
|
538
|
-
if foreign.is_a?(Symbol) || foreign.is_a?(String)
|
|
539
|
-
raise ArgumentError.new(
|
|
540
|
-
"Using a symbol/string for `#{option_sym}` is no longer supported. Instead, use a Proc " \
|
|
541
|
-
"that returns the class, e.g., foreign: -> {Foo}"
|
|
542
|
-
)
|
|
543
|
-
end
|
|
544
|
-
|
|
545
|
-
if !foreign.is_a?(Proc) && !foreign.is_a?(Array) && !foreign.respond_to?(:load)
|
|
546
|
-
raise ArgumentError.new("The `#{option_sym}` option must be #{valid_type_msg}")
|
|
547
|
-
end
|
|
548
|
-
end
|
|
549
|
-
|
|
550
|
-
# checked(:never) - Rules hash is expensive to check
|
|
551
|
-
sig do
|
|
552
|
-
params(
|
|
553
|
-
prop_name: T.any(String, Symbol),
|
|
554
|
-
rules: Rules,
|
|
555
|
-
foreign: T.untyped,
|
|
556
|
-
)
|
|
557
|
-
.void
|
|
558
|
-
.checked(:never)
|
|
559
|
-
end
|
|
560
|
-
private def define_foreign_method(prop_name, rules, foreign)
|
|
561
|
-
fk_method = "#{prop_name}_"
|
|
562
|
-
|
|
563
|
-
# n.b. there's no clear reason *not* to allow additional options
|
|
564
|
-
# here, but we're baking in `allow_direct_mutation` since we
|
|
565
|
-
# *haven't* allowed additional options in the past and want to
|
|
566
|
-
# default to keeping this interface narrow.
|
|
567
|
-
foreign = T.let(foreign, T.untyped, checked: false)
|
|
568
|
-
@class.send(:define_method, fk_method) do |allow_direct_mutation: nil|
|
|
569
|
-
if foreign.is_a?(Proc)
|
|
570
|
-
resolved_foreign = foreign.call
|
|
571
|
-
if !resolved_foreign.respond_to?(:load)
|
|
572
|
-
raise ArgumentError.new(
|
|
573
|
-
"The `foreign` proc for `#{prop_name}` must return a model class. " \
|
|
574
|
-
"Got `#{resolved_foreign.inspect}` instead."
|
|
575
|
-
)
|
|
576
|
-
end
|
|
577
|
-
# `foreign` is part of the closure state, so this will persist to future invocations
|
|
578
|
-
# of the method, optimizing it so this only runs on the first invocation.
|
|
579
|
-
foreign = resolved_foreign
|
|
580
|
-
end
|
|
581
|
-
opts = if allow_direct_mutation.nil?
|
|
582
|
-
{}
|
|
583
|
-
else
|
|
584
|
-
{allow_direct_mutation: allow_direct_mutation}
|
|
585
|
-
end
|
|
586
|
-
|
|
587
|
-
T.unsafe(self.class).decorator.foreign_prop_get(self, prop_name, foreign, rules, opts)
|
|
588
|
-
end
|
|
589
|
-
|
|
590
|
-
force_fk_method = "#{fk_method}!"
|
|
591
|
-
@class.send(:define_method, force_fk_method) do |allow_direct_mutation: nil|
|
|
592
|
-
loaded_foreign = send(fk_method, allow_direct_mutation: allow_direct_mutation)
|
|
593
|
-
if !loaded_foreign
|
|
594
|
-
raise("Failed to load foreign model method=#{force_fk_method} class=#{self.class}")
|
|
595
|
-
end
|
|
596
|
-
loaded_foreign
|
|
597
|
-
end
|
|
598
|
-
end
|
|
599
|
-
|
|
600
|
-
# checked(:never) - Rules hash is expensive to check
|
|
601
|
-
sig do
|
|
602
|
-
params(
|
|
603
|
-
prop_name: Symbol,
|
|
604
|
-
prop_cls: T::Module[T.anything],
|
|
605
|
-
rules: Rules,
|
|
606
|
-
foreign: T.untyped,
|
|
607
|
-
)
|
|
608
|
-
.void
|
|
609
|
-
.checked(:never)
|
|
610
|
-
end
|
|
611
|
-
private def handle_foreign_option(prop_name, prop_cls, rules, foreign)
|
|
612
|
-
validate_foreign_option(
|
|
613
|
-
:foreign, foreign, valid_type_msg: "a model class or a Proc that returns one"
|
|
614
|
-
)
|
|
615
|
-
|
|
616
|
-
if prop_cls != String
|
|
617
|
-
raise ArgumentError.new("`foreign` can only be used with a prop type of String")
|
|
618
|
-
end
|
|
619
|
-
|
|
620
|
-
if foreign.is_a?(Array)
|
|
621
|
-
# We don't support arrays with `foreign` because it's hard to both preserve ordering and
|
|
622
|
-
# keep them from being lurky performance hits by issuing a bunch of un-batched DB queries.
|
|
623
|
-
# We could potentially address that by porting over something like AmbiguousIDLoader.
|
|
624
|
-
raise ArgumentError.new(
|
|
625
|
-
"Using an array for `foreign` is no longer supported. Instead, please use a union type of " \
|
|
626
|
-
"token types for the prop type, e.g., T.any(Opus::Autogen::Tokens::FooModelToken, Opus::Autogen::Tokens::BarModelToken)"
|
|
627
|
-
)
|
|
628
|
-
end
|
|
629
|
-
|
|
630
|
-
unless foreign.is_a?(Proc)
|
|
631
|
-
raise ArgumentError.new(<<~MESSAGE)
|
|
632
|
-
Please use a Proc that returns a model class instead of the model class itself as the argument to `foreign`. In other words:
|
|
633
|
-
|
|
634
|
-
instead of `prop :foo, String, foreign: FooModel`
|
|
635
|
-
use `prop :foo, String, foreign: -> {FooModel}`
|
|
636
|
-
|
|
637
|
-
MESSAGE
|
|
638
|
-
end
|
|
639
|
-
|
|
640
|
-
define_foreign_method(prop_name, rules, foreign)
|
|
641
|
-
end
|
|
642
|
-
|
|
643
509
|
# TODO: rename this to props_inherited
|
|
644
510
|
#
|
|
645
511
|
# This gets called when a module or class that extends T::Props gets included, extended,
|