-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
78 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,96 @@ | ||
# NullAssociation | ||
# null_association | ||
|
||
TODO: Delete this and the text below, and describe your gem | ||
[![CI](https://github.com/keygen-sh/null_association/actions/workflows/test.yml/badge.svg)](https://github.com/keygen-sh/null_association/actions) | ||
[![Gem Version](https://badge.fury.io/rb/null_association.svg)](https://badge.fury.io/rb/null_association) | ||
|
||
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/null_association`. To experiment with that code, run `bin/console` for an interactive prompt. | ||
Use `null_association` to utilize the [null object pattern](https://en.wikipedia.org/wiki/Null_object_pattern) | ||
with Active Record associations. | ||
|
||
This gem was extracted from [Keygen](https://keygen.sh). | ||
|
||
Sponsored by: | ||
|
||
<a href="https://keygen.sh?ref=null_association"> | ||
<div> | ||
<img src="https://keygen.sh/images/logo-pill.png" width="200" alt="Keygen"> | ||
</div> | ||
</a> | ||
|
||
_A fair source software licensing and distribution API._ | ||
|
||
## Installation | ||
|
||
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. | ||
Add this line to your application's `Gemfile`: | ||
|
||
Install the gem and add to the application's Gemfile by executing: | ||
```ruby | ||
gem 'null_association' | ||
``` | ||
|
||
$ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG | ||
And then execute: | ||
|
||
If bundler is not being used to manage dependencies, install the gem by executing: | ||
```bash | ||
$ bundle | ||
``` | ||
|
||
$ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG | ||
Or install it yourself as: | ||
|
||
```bash | ||
$ gem install null_association | ||
``` | ||
|
||
## Usage | ||
|
||
TODO: Write usage instructions here | ||
To use a null object, define an optional singular association and use the | ||
`null_object:` keyword, which accepts a class, a string, or an instance. When | ||
the association is nil, the null object will be returned instead. | ||
|
||
```ruby | ||
class NullPlan | ||
include Singleton | ||
|
||
def name = 'Free' | ||
def free? = true | ||
def pro? = false | ||
def ent? = false | ||
end | ||
``` | ||
|
||
```ruby | ||
class Account | ||
belongs_to :plan, optional: true, null_object: NullPlan.instance | ||
has_one :billing, null_object: NullBilling.instance | ||
end | ||
``` | ||
|
||
## Development | ||
```ruby | ||
account = Account.create(plan: nil) | ||
|
||
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. | ||
puts account.plan # => #<NullPlan name="Free"> | ||
puts account.plan.free? # => true | ||
puts account.plan.ent? # => false | ||
|
||
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). | ||
account.update(plan: Plan.new(name: 'Ent', ent: true)) | ||
|
||
puts account.plan # => #<Plan id=1 name="Ent"> | ||
puts account.plan.free? # => false | ||
puts account.plan.ent? # => true | ||
``` | ||
|
||
## Supported Rubies | ||
|
||
**`null_association` supports Ruby 3.1 and above.** We encourage you to upgrade | ||
if you're on an older version. Ruby 3 provides a lot of great features, like | ||
better pattern matching and a new shorthand hash syntax. | ||
|
||
## Is it any good? | ||
|
||
Yes. | ||
|
||
## Contributing | ||
|
||
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/null_association. | ||
If you have an idea, or have discovered a bug, please open an issue or create a | ||
pull request. | ||
|
||
## License | ||
|
||
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). |