sorbet_typed-props 1.0.4 → 1.1.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 +4 -4
- data/.cz.yaml +1 -1
- data/CHANGELOG.md +11 -0
- data/README.md +50 -0
- data/lib/sorbet_typed/props/version.rb +1 -1
- data/lib/sorbet_typed/props.rb +16 -0
- metadata +4 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2c457e7b60e68c291d4b327324fb3a252f8eda2b2aff2387d4673f351cb3b7fb
|
|
4
|
+
data.tar.gz: e39071b28288af5469881d35b5872997b988c63b4161ca75561ca8e58f178ef6
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 907229513f1e616e0e7b5421485fcb02e20c62d8ae053615fbfafbbfca28c6afa63fedc5dd980c6e7e30f1a697eb38402a1b76944359c8b1038c7db1b685e388
|
|
7
|
+
data.tar.gz: 0e7eca8bf03774d1504603435c581b9cf40940bd04fcf602e310a7fa105cc61b34ac63d09d6f2330fb5a13f64572c057211400308042aa816a8b9eb9d2a35ee5
|
data/.cz.yaml
CHANGED
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
|
@@ -17,6 +17,8 @@ in parallel).
|
|
|
17
17
|
- [Installation](#installation)
|
|
18
18
|
- [Usage](#usage)
|
|
19
19
|
- [Phlex Example](#phlex-example)
|
|
20
|
+
- [Instance Variables](#instance-variables)
|
|
21
|
+
- [Extending initializer logic](#extending-initializer-logic)
|
|
20
22
|
- [Method Visibility](#method-visibility)
|
|
21
23
|
- [Development](#development)
|
|
22
24
|
- [Contributing](#contributing)
|
|
@@ -68,6 +70,54 @@ class Components::HelloWorld < Phlex::HTML
|
|
|
68
70
|
end
|
|
69
71
|
```
|
|
70
72
|
|
|
73
|
+
### Instance Variables
|
|
74
|
+
|
|
75
|
+
If you need more instance variables beside defined props, define them within the class body, not the initializer.
|
|
76
|
+
|
|
77
|
+
```ruby
|
|
78
|
+
class MyClass
|
|
79
|
+
extend T::Sig
|
|
80
|
+
include SorbetTyped::Props
|
|
81
|
+
|
|
82
|
+
prop :my_prop, Integer
|
|
83
|
+
|
|
84
|
+
@my_var = T.let(nil, T.nilable(String))
|
|
85
|
+
|
|
86
|
+
sig { void }
|
|
87
|
+
def foo
|
|
88
|
+
T.reveal_type(my_prop) # => Integer
|
|
89
|
+
T.reveal_type(@my_var) # => T.nilable(String)
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Extending initializer logic
|
|
95
|
+
|
|
96
|
+
If you want to implement some logic on instance initialization, implementing a custom `initializer` method would require
|
|
97
|
+
you to write a lot of boilerplate code to redefine the method signature and instance variable initialization normally
|
|
98
|
+
done by `SorbetTyped::Props`.
|
|
99
|
+
|
|
100
|
+
To allow you to nonetheless extend initialization, `SorbetTyped::Props` implements the overridable method
|
|
101
|
+
`post_props_initialize`. It gets called right after prop initialization and has access to all props to do validation or
|
|
102
|
+
whatever you like. Override this method in your own class to implement whatever custom logic you need.
|
|
103
|
+
|
|
104
|
+
```ruby
|
|
105
|
+
class MyClass
|
|
106
|
+
extend T::Sig
|
|
107
|
+
include SorbetTyped::Props
|
|
108
|
+
|
|
109
|
+
prop :my_prop, Integer
|
|
110
|
+
|
|
111
|
+
sig { override.void }
|
|
112
|
+
def post_props_initialize
|
|
113
|
+
raise 'invalid' if my_prop.negative?
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
**Note:** you cannot do instance variable initialization here. Define them [within the class body](#instance-variables)
|
|
119
|
+
instead.
|
|
120
|
+
|
|
71
121
|
### Method Visibility
|
|
72
122
|
|
|
73
123
|
If you want attributes in your initializer but not be part of your public class interface, you can use ruby's visibility
|
data/lib/sorbet_typed/props.rb
CHANGED
|
@@ -9,12 +9,28 @@ module SorbetTyped
|
|
|
9
9
|
# interface usable. It's just a wrapper around T::Props and
|
|
10
10
|
# T::Props::Constructor. It's also the ancestor the custom tapioca compiler
|
|
11
11
|
# looks for when generating initializer type annotation rbis.
|
|
12
|
+
#
|
|
13
|
+
# :reek:ModuleInitialize -- we accept this here. This module defines an
|
|
14
|
+
# initialize method via T::Props::Constructor nonetheless, we only extend it.
|
|
15
|
+
# It shouldn't really be used with other initializers anyway.
|
|
12
16
|
module Props
|
|
17
|
+
extend T::Sig
|
|
13
18
|
extend T::Helpers
|
|
14
19
|
|
|
15
20
|
include T::Props
|
|
16
21
|
include T::Props::Constructor
|
|
17
22
|
|
|
18
23
|
mixes_in_class_methods(T::Props::ClassMethods)
|
|
24
|
+
|
|
25
|
+
sig { params(args: T.anything, kwargs: T.anything).void }
|
|
26
|
+
def initialize(*args, **kwargs)
|
|
27
|
+
super
|
|
28
|
+
post_props_initialize
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
sig { overridable.void }
|
|
34
|
+
def post_props_initialize; end
|
|
19
35
|
end
|
|
20
36
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: sorbet_typed-props
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0
|
|
4
|
+
version: 1.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Richard Kramer
|
|
@@ -18,7 +18,7 @@ dependencies:
|
|
|
18
18
|
version: 0.6.0
|
|
19
19
|
- - "<="
|
|
20
20
|
- !ruby/object:Gem::Version
|
|
21
|
-
version: 0.6.
|
|
21
|
+
version: 0.6.12784
|
|
22
22
|
type: :runtime
|
|
23
23
|
prerelease: false
|
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
|
@@ -28,7 +28,7 @@ dependencies:
|
|
|
28
28
|
version: 0.6.0
|
|
29
29
|
- - "<="
|
|
30
30
|
- !ruby/object:Gem::Version
|
|
31
|
-
version: 0.6.
|
|
31
|
+
version: 0.6.12784
|
|
32
32
|
- !ruby/object:Gem::Dependency
|
|
33
33
|
name: tapioca
|
|
34
34
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -75,6 +75,7 @@ metadata:
|
|
|
75
75
|
homepage_uri: https://gitlab.com/sorbet_typed/props
|
|
76
76
|
source_code_uri: https://gitlab.com/sorbet_typed/props
|
|
77
77
|
rubygems_mfa_required: 'true'
|
|
78
|
+
changelog_uri: https://gitlab.com/sorbet_typed/props/-/blob/main/CHANGELOG.md
|
|
78
79
|
rdoc_options: []
|
|
79
80
|
require_paths:
|
|
80
81
|
- lib
|