From 5917303c94eaa9e740ead61a657ef53f3196e2c2 Mon Sep 17 00:00:00 2001 From: Zeke Gabrielse Date: Thu, 15 Aug 2024 13:09:49 -0500 Subject: [PATCH] update readme --- README.md | 91 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 78 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 01ef12d..e47b53c 100644 --- a/README.md +++ b/README.md @@ -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: + + +
+ Keygen +
+
+ +_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 # => # +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 # => # +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).