Skip to content

Commit d870047

Browse files
committed
Initial README
1 parent 47317f9 commit d870047

2 files changed

Lines changed: 107 additions & 16 deletions

File tree

README.md

Lines changed: 106 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,126 @@
11
# TypeToolkit
22

3-
TODO: Delete this and the text below, and describe your gem
3+
[💎 RubyGems](https://rubygems.org/gems/type_toolkit)
44

5-
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/type_toolkit`. To experiment with that code, run `bin/console` for an interactive prompt.
5+
A minimal runtime library for implementing abstract classes, interfaces, and more.
66

77
## Installation
88

9-
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
9+
Install Type Toolkit into your bundle, add it to your `Gemfile`:
1010

11-
Install the gem and add to the application's Gemfile by executing:
11+
```rb
12+
gem "type_toolkit"
13+
```
14+
15+
And then run `bundle install`.
16+
17+
### RuboCop Cops
18+
19+
This gem ships with RuboCop cops that we recommend you enable for your application. You can do so by adding it to the `plugins` list of your `rubocop.yml`:
20+
21+
```yml
22+
plugins:
23+
- rubocop-other-extension
24+
- rubocop-type_toolkit
25+
```
26+
27+
### Cherry-picking features
28+
29+
Simply writing `gem "type_toolkit"` in your `Gemfile` will grab all the tools from the toolkit, which we highly recommend. This adds methods to Ruby's core classes, to make them feel like a native part of the language.
1230

13-
```bash
14-
bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
31+
Alternatively, you can cherry-pick only the tools you need. For example, if you only want to use the `not_nil!` assertion, you can add the following to your `Gemfile`:
32+
33+
```rb
34+
gem "type_toolkit", require: ["type_toolkit/ext/nil_assertions"]
1535
```
1636

17-
If bundler is not being used to manage dependencies, install the gem by executing:
37+
or you can skip the require in the `Gemfile`, and later manually require it in a specific file:
1838

19-
```bash
20-
gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
39+
```ruby
40+
gem "type_toolkit", require: false
2141
```
2242

23-
## Usage
43+
```ruby
44+
# your_script.rb
45+
require "type_toolkit/ext/nil_assertions"
46+
```
2447

25-
TODO: Write usage instructions here
48+
49+
## Tools
50+
51+
### `not_nil!` assertion
52+
53+
When debugging a `nil`-related error, it can be difficult to trace back where the `nil` actually originated from. It could have come in from a parameter, whose argument was read from an instance variable, on an object loaded from a cache, populated by some totally different request.
54+
55+
If a value can't be nil, it's best for that to be clearly asserted as close to where that nilable value was first generated. That way, a rogue `nil` isn't allowed to propagate arbitrarily far away in downstream code.
56+
57+
Type Toolkit provides a `not_nil!` assertion, which will raise an `UnexpectedNilError` if the receiver is `nil`.
58+
59+
```rb
60+
# `__dir__` can be nil in an "eval", but never in a Ruby file.
61+
gemfile = Pathname.new(__dir__.not_nil!) / "Gemfile"
62+
```
63+
64+
`not_nil!` method calls can be chained, to fail early if any value in the chain is `nil`:
65+
66+
```rb
67+
last_delivery = user.not_nil!
68+
.orders.last.not_nil!
69+
.deliveries.last.not_nil!
70+
```
71+
72+
## Guiding Principles
73+
74+
### Blazingly fast™
75+
76+
All tools should aim to have 0 overhead at runtime, as compared to hand-written Ruby.
77+
78+
### Pay only for what you use
79+
80+
There should be no performance cost for a tool that you're not using.
81+
82+
Wherever there's an unavoidable cost, it should only ever apply to code that actually uses the tool. **No global costs.**
83+
84+
Tools should be optimized for the common case, even if that slows them in rare cases. For example, calling an abstract method is as fast as calling any other method, but only if it's implemented. Calling an unimplemented method call hits the `#method_missing` path, which is significantly slower. This is an acceptable trade-off, because no production code should be calling `#method_missing`, anyway.
85+
86+
In all cases, performance costs are quantified and well-documented.
87+
88+
### Feels like Ruby
89+
90+
Tools should feel like part of a programming language itself, and less like its standard library. Type Toolkit takes on the implementation complexity so your code doesn't have to.
91+
92+
To keep that bar high, the toolkit is lean and deliberately focused on
93+
language-level primitives. Where practical, new
94+
features should be built in separate libraries that _use_ the Type Toolkit.
2695

2796
## Development
2897

29-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
98+
This repo has Rake tasks configured for common development tasks:
99+
1. `bundle exec rake` to do the next 3 steps all together
100+
1. `bundle exec rake rubocop` for linting
101+
1. `bundle exec rake typecheck` to typecheck with Sorbet
102+
1. `bundle exec rake test` to run all the tests
103+
1. `bundle exec rake -T` to list all the tasks
104+
105+
### Releasing
106+
107+
This gem is automatically released to [RubyGems.org](https://rubygems.org/gems/type_toolkit) via [Trusted Publishing](https://guides.rubygems.org/trusted-publishing/). To publish a new version of the gem, all you have to do is:
108+
109+
1. Update the version number in [`version.rb`](https://github.com/Shopify/type_toolkit/blob/main/lib/type_toolkit/version.rb)
110+
* This can either be part of an existing PR, or a new standalone PR.
111+
112+
2. Once that PR is merged, create a tag at the new head of the `main` branch:
113+
114+
```sh
115+
git pull origin main && git checkout main && git tag v1.2.3
116+
```
117+
118+
3. Push the new tag.
30119

31-
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).
120+
```sh
121+
git push origin v1.2.3
122+
```
32123

33-
## Contributing
124+
This will automatically trigger our [release workflow](https://github.com/Shopify/type_toolkit/actions/workflows/release.yml). It must be approved by a member of the Ruby and Rails Infrastructure team at Shopify before it will run.
34125

35-
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/type_toolkit.
126+
Once approved, the workflow will automatically publish the new gem version to RubyGems.org, and create a new [GitHub release](https://github.com/Shopify/type_toolkit/releases).

sorbet/config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
--ignore=tmp/
44
--ignore=vendor/
55
--enable-experimental-rbs-comments
6-
--suppress-payload-superclass-redefinition-for=RDoc::Markup::Heading
6+
--suppress-payload-superclass-redefinition-for=RDoc::Markup::Heading

0 commit comments

Comments
 (0)