typed_struct 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +2 -2
- data/README.md +7 -3
- data/lib/typed_struct/version.rb +1 -1
- data/lib/typed_struct.rb +13 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8b464562db650de5a4d3026ca8806d85b993c8f6bfcf3e86d1988c859432a427
|
4
|
+
data.tar.gz: a6d38114357f8657b675074a36cbfba6129695eb80bc1b9333219f241cc44b79
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b280075f73bf18618ea3ab207b9d3f632928d69a364902b3374ab4b4a728ba791874247a7d36502fd4c83e00f980e4e35e7b777d8e80a40223377a207a03432c
|
7
|
+
data.tar.gz: 9ba0515a1401a4be3d0feaa4bd79fe1dee3c7d20c1d84990485c0eb411df88df16d3bb86a5703f13fca71299e925c6b0e157932a1691efdb10987be536cd1d2d
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
typed_struct (0.1.
|
4
|
+
typed_struct (0.1.2)
|
5
5
|
rbs (~> 1.0)
|
6
6
|
|
7
7
|
GEM
|
@@ -9,7 +9,7 @@ GEM
|
|
9
9
|
specs:
|
10
10
|
diff-lcs (1.4.4)
|
11
11
|
rake (13.0.3)
|
12
|
-
rbs (1.
|
12
|
+
rbs (1.5.1)
|
13
13
|
rspec (3.10.0)
|
14
14
|
rspec-core (~> 3.10.0)
|
15
15
|
rspec-expectations (~> 3.10.0)
|
data/README.md
CHANGED
@@ -9,6 +9,8 @@ require 'typed_struct' # unless using rails
|
|
9
9
|
|
10
10
|
User = TypedStruct.new name: String, # an instance of String
|
11
11
|
age: Integer, # an instance of Integer
|
12
|
+
username: /\w{4,}/, # must match given Regexp
|
13
|
+
rating: (0..5), # must be value from 0 to 5
|
12
14
|
type: "User", # must by a string with value "User"
|
13
15
|
interests: Rbs("Array[String]"), # an RBS generic type (an Array of Strings)
|
14
16
|
preferences: Rbs("{ opt_out_of_emails: bool, additional: untyped }") # RBS record type
|
@@ -17,7 +19,9 @@ clive = User.new name: "Clive",
|
|
17
19
|
age: 22,
|
18
20
|
interests: %w[surfing skiing],
|
19
21
|
preferences: { opt_out_of_emails: true, additional: { preferred_theme: :dark } },
|
20
|
-
type: "User"
|
22
|
+
type: "User",
|
23
|
+
rating: 4,
|
24
|
+
username: "cliveabc"
|
21
25
|
|
22
26
|
clive.age # 22
|
23
27
|
clive.age = '22' # => Error
|
@@ -27,7 +31,7 @@ clive.freeze # no more changes can be made
|
|
27
31
|
|
28
32
|
Note that a `TypedStruct` inherits from `Struct` directly, so anything from `Struct` is also available in `TypedStruct` - see [Struct docs](https://ruby-doc.org/core-3.0.1/Struct.html) for more info.
|
29
33
|
|
30
|
-
**See [RBS reference](https://github.com/ruby/rbs/blob/3c046c77c3006211a1a14eedc35221ac4198f788/docs/syntax.md) for
|
34
|
+
**See [RBS reference](https://github.com/ruby/rbs/blob/3c046c77c3006211a1a14eedc35221ac4198f788/docs/syntax.md) for more info on writing RBS types**
|
31
35
|
|
32
36
|
## Installation
|
33
37
|
|
@@ -53,7 +57,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
53
57
|
|
54
58
|
## Contributing
|
55
59
|
|
56
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
60
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/johansenja/typed_struct.
|
57
61
|
|
58
62
|
## License
|
59
63
|
|
data/lib/typed_struct/version.rb
CHANGED
data/lib/typed_struct.rb
CHANGED
@@ -7,8 +7,20 @@ require "rbs"
|
|
7
7
|
class TypedStruct < Struct
|
8
8
|
include TypeChecking
|
9
9
|
|
10
|
+
OVERRIDING_NATIVE_METHOD_MSG =
|
11
|
+
"*** WARNING *** property %s overrides a native method in #{name}. Consider using something else (called from %s)".freeze
|
12
|
+
|
13
|
+
# any methods which are able to be overridden
|
14
|
+
alias_method :__class__, :class
|
15
|
+
|
10
16
|
class << self
|
11
17
|
def new(**properties)
|
18
|
+
properties.each_key do |prop|
|
19
|
+
if method_defined?(prop)
|
20
|
+
$stdout.puts OVERRIDING_NATIVE_METHOD_MSG % [prop.inspect, caller(3).first]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
12
24
|
super(*properties.keys, keyword_init: true).tap do |klass|
|
13
25
|
klass.class.instance_eval do
|
14
26
|
include TypeChecking
|
@@ -38,7 +50,7 @@ class TypedStruct < Struct
|
|
38
50
|
end
|
39
51
|
|
40
52
|
def initialize(**attrs)
|
41
|
-
opts = self.
|
53
|
+
opts = self.__class__.options
|
42
54
|
opts[:types].each do |prop, expected_type|
|
43
55
|
passed_value = attrs[prop]
|
44
56
|
next if val_is_type? passed_value, expected_type
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: typed_struct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joseph Johansen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-09-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rbs
|