Skip to content

Commit

Permalink
More docs for for_gem_extension
Browse files Browse the repository at this point in the history
  • Loading branch information
fxn committed Apr 28, 2023
1 parent e07c1e5 commit 8842758
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -421,9 +421,25 @@ Let's suppose you are writing a gem to extend `Net::HTTP` with some niche featur
* The entry point should be `lib/net/http/niche_feature.rb`.
* Optionally, the gem could have a top-level `lib/net-http-niche_feature.rb`, but, if defined, that one should have just a `require` call for the entry point.

Gem extensions following the conventions above have a dedicated loader constructor:
The top-level file mentioned in the last point is optional. In particular, from

```ruby
gem "net-http-niche_feature"
```

if the dasherized file does not exist, Bundler notes the conventional dasherized pattern and issues a `require` for `net/http/niche_feature`.

Gem extensions following the conventions above have a dedicated loader constructor: `Zeitwerk::Loader.for_gem_extension`.

The structure of the gem would be like this:

```ruby
# lib/net-http-niche_feature.rb (optional)

# For technical reasons, this cannot be require_relative.
require "net/http/niche_feature"


# lib/net/http/niche_feature.rb

require "net/http"
Expand All @@ -437,6 +453,13 @@ module Net::HTTP::NicheFeature
# to reference project constants, in this case Net::HTTP::NicheFeature::MyMixin.
include MyMixin
end


# lib/net/http/niche_feature/version.rb

module Net::HTTP::NicheFeature
VERSION = "1.0.0"
end
```

`Zeitwerk::Loader.for_gem_extension` expects as argument the namespace being extended, which has to be a non-anonymous class or module object.
Expand Down

0 comments on commit 8842758

Please sign in to comment.