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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c3375fc3fde1c42faa3d3ab97fd452cbe00bc869928d152a0ea67af139aab30d
4
- data.tar.gz: b4981b8264cc69a678dca36c666ee10ee82371e451ba067e558334240621de6a
3
+ metadata.gz: c314d5e68394fa4ae9465f421cd0d0a36c1b1aa08c011113a6be80236779fddb
4
+ data.tar.gz: 9f29964aaee3d86cc0ba773cf54977d95ced2df7c8bb98955099e5681cd6bfd3
5
5
  SHA512:
6
- metadata.gz: e823becbc89695f9e7cd9e6a2c494750b5507ffd6cf6d638089007e4f1294950b140d73f4494e7edc592f1116389f4800312c406913767c2b53076e718dddb56
7
- data.tar.gz: 6fd49203aec3eee88359bc7bb94dcc2782387c47c11f3bd4f8c53b20bfec76778061c8632575f526396795098e13d0833cbc23987a28a9667c0b72fd8785438f
6
+ metadata.gz: dfd2ce5fac539dd38780925f2771476acdd71ffd18bb2945f65377f6603556764a38633a311b531a3c5e808dc89f5e93b7ab3ac3a6f3d82f7aa2f769d38d3294
7
+ data.tar.gz: 9a39c71af23167b16280c61f0ff1ee1adcb7f01e7dd8e35a80433def24bfb7f3086dd14022f0bc69f54fb6a22b208e833671f0bf92b5ea368e3eac801f5735e5
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- typed_struct (0.1.0)
4
+ typed_struct (0.1.1)
5
5
  rbs (~> 1.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,8 +1,33 @@
1
1
  # TypedStruct
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/typed_struct`. To experiment with that code, run `bin/console` for an interactive prompt.
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
- TODO: Delete this and the text above, and describe your gem
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
- prop = properties[k]
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class TypedStruct < Struct
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: typed_struct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joseph Johansen