typed_struct 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +27 -6
- data/lib/typed_struct.rb +11 -6
- data/lib/typed_struct/version.rb +1 -1
- 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: c314d5e68394fa4ae9465f421cd0d0a36c1b1aa08c011113a6be80236779fddb
|
4
|
+
data.tar.gz: 9f29964aaee3d86cc0ba773cf54977d95ced2df7c8bb98955099e5681cd6bfd3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dfd2ce5fac539dd38780925f2771476acdd71ffd18bb2945f65377f6603556764a38633a311b531a3c5e808dc89f5e93b7ab3ac3a6f3d82f7aa2f769d38d3294
|
7
|
+
data.tar.gz: 9a39c71af23167b16280c61f0ff1ee1adcb7f01e7dd8e35a80433def24bfb7f3086dd14022f0bc69f54fb6a22b208e833671f0bf92b5ea368e3eac801f5735e5
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,33 @@
|
|
1
1
|
# TypedStruct
|
2
2
|
|
3
|
-
|
3
|
+
A Typed Struct is a lightweight data structure inspired by [Dry Struct](https://github.com/dry-rb/dry-struct), which allows for reading and writing properties while making use of a flexible and powerful type checking system, which also incorporates Ruby's [RBS](https://github.com/ruby/rbs/) for type definitions.
|
4
4
|
|
5
|
-
|
5
|
+
## Example
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
require 'typed_struct' # unless using rails
|
9
|
+
|
10
|
+
User = TypedStruct.new name: String, # an instance of String
|
11
|
+
age: Integer, # an instance of Integer
|
12
|
+
type: "User", # must by a string with value "User"
|
13
|
+
interests: Rbs("Array[String]"), # an RBS generic type (an Array of Strings)
|
14
|
+
preferences: Rbs("{ opt_out_of_emails: bool, additional: untyped }") # RBS record type
|
15
|
+
|
16
|
+
clive = User.new name: "Clive",
|
17
|
+
age: 22,
|
18
|
+
interests: %w[surfing skiing],
|
19
|
+
preferences: { opt_out_of_emails: true, additional: { preferred_theme: :dark } },
|
20
|
+
type: "User"
|
21
|
+
|
22
|
+
clive.age # 22
|
23
|
+
clive.age = '22' # => Error
|
24
|
+
clive.preferences = { "opt_out_of_emails" => true, "additional" => nil } # error - type mismatch, not Symbol keys
|
25
|
+
clive.freeze # no more changes can be made
|
26
|
+
```
|
27
|
+
|
28
|
+
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
|
+
|
30
|
+
**See [RBS reference](https://github.com/ruby/rbs/blob/3c046c77c3006211a1a14eedc35221ac4198f788/docs/syntax.md) for my info on writing RBS types**
|
6
31
|
|
7
32
|
## Installation
|
8
33
|
|
@@ -20,10 +45,6 @@ Or install it yourself as:
|
|
20
45
|
|
21
46
|
$ gem install typed_struct
|
22
47
|
|
23
|
-
## Usage
|
24
|
-
|
25
|
-
TODO: Write usage instructions here
|
26
|
-
|
27
48
|
## Development
|
28
49
|
|
29
50
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/typed_struct.rb
CHANGED
@@ -17,14 +17,19 @@ class TypedStruct < Struct
|
|
17
17
|
|
18
18
|
klass.instance_eval do
|
19
19
|
@options = { types: properties }
|
20
|
+
|
21
|
+
define_method :[]= do |key, val|
|
22
|
+
prop = properties[key]
|
23
|
+
unless val_is_type? val, prop
|
24
|
+
raise "Unexpected type #{val.class} for #{key.inspect} (expected #{prop})"
|
25
|
+
end
|
26
|
+
|
27
|
+
super key, val
|
28
|
+
end
|
29
|
+
|
20
30
|
properties.each_key do |k|
|
21
31
|
define_method :"#{k}=" do |val|
|
22
|
-
|
23
|
-
if val_is_type? val, prop
|
24
|
-
raise "Unexpected type #{val.class} for #{k.inspect} (expected #{prop})"
|
25
|
-
end
|
26
|
-
# it seems that structs behave in such a way that you can't call super here, but it
|
27
|
-
# will still set the value anyway
|
32
|
+
self[k] = val
|
28
33
|
end
|
29
34
|
end
|
30
35
|
end
|
data/lib/typed_struct/version.rb
CHANGED