https://github.com/johansenja/typed_struct
Ruby structs but with type-checked attributes ⚡️🔐
https://github.com/johansenja/typed_struct
rbs ruby struct type-checking type-definitions
Last synced: 2 months ago
JSON representation
Ruby structs but with type-checked attributes ⚡️🔐
- Host: GitHub
- URL: https://github.com/johansenja/typed_struct
- Owner: johansenja
- License: mit
- Created: 2021-06-21T20:21:14.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2022-12-25T20:19:02.000Z (over 2 years ago)
- Last Synced: 2025-03-17T06:11:42.552Z (2 months ago)
- Topics: rbs, ruby, struct, type-checking, type-definitions
- Language: Ruby
- Homepage:
- Size: 24.4 KB
- Stars: 15
- Watchers: 4
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# TypedStruct
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.
## Example
```ruby
require 'typed_struct' # unless using railsUser = TypedStruct.new name: String, # an instance of String
age: Integer, # an instance of Integer
username: /\w{4,}/, # must match given Regexp
rating: (0..5), # must be value from 0 to 5
type: "User", # must by a string with value "User"
interests: Rbs("Array[String]"), # an RBS generic type (an Array of Strings)
preferences: Rbs("{ opt_out_of_emails: bool, additional: untyped }") # RBS record typeclive = User.new name: "Clive",
age: 22,
interests: %w[surfing skiing],
preferences: { opt_out_of_emails: true, additional: { preferred_theme: :dark } },
type: "User",
rating: 4,
username: "cliveabc"clive.age # 22
clive.age = '22' # => Error
clive.preferences = { "opt_out_of_emails" => true, "additional" => nil } # error - type mismatch, not Symbol keys
clive.freeze # no more changes can be made
```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.
**See [RBS reference](https://github.com/ruby/rbs/blob/3c046c77c3006211a1a14eedc35221ac4198f788/docs/syntax.md) for more info on writing RBS types**
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'typed_struct'
```And then execute:
$ bundle install
Or install it yourself as:
$ gem install typed_struct
## Development
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.
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/johansenja/typed_struct.
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).