Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/keygen-sh/null_association
Use the null object pattern with Active Record associations.
https://github.com/keygen-sh/null_association
Last synced: 19 days ago
JSON representation
Use the null object pattern with Active Record associations.
- Host: GitHub
- URL: https://github.com/keygen-sh/null_association
- Owner: keygen-sh
- License: mit
- Created: 2024-07-26T19:10:37.000Z (about 2 months ago)
- Default Branch: master
- Last Pushed: 2024-08-16T21:54:34.000Z (about 1 month ago)
- Last Synced: 2024-08-16T23:48:19.690Z (about 1 month ago)
- Language: Ruby
- Homepage:
- Size: 12.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Security: SECURITY.md
Awesome Lists containing this project
README
# null_association
[![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)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 fair source software licensing and distribution API._
## Installation
Add this line to your application's `Gemfile`:
```ruby
gem 'null_association'
```And then execute:
```bash
$ bundle
```Or install it yourself as:
```bash
$ gem install null_association
```## Usage
To use a null object, define an optional singular association and use the
`null_object:` keyword, which accepts a class name, a class, or an instance.
When the association is nil, the null object will be returned instead.```ruby
class NullObject
include Singletondef present? = false
def blank? = true
end
``````ruby
class NullPlan < NullObject
def name = 'Free'
def free? = true
def pro? = false
def ent? = false
end
``````ruby
class NullBilling < NullObject
def subscribed? = true
def trialing? = false
def canceled? = false
end
``````ruby
class Account
belongs_to :plan, optional: true, null_object: NullPlan.instance
has_one :billing, null_object: NullBilling.instance
end
``````ruby
account = Account.create(plan: nil)puts account.plan # => #
puts account.plan.free? # => true
puts account.plan.ent? # => falseaccount.update(plan: Plan.new(name: 'Ent', ent: true))
puts account.plan # => #
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
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).