Skip to content

Commit

Permalink
a bit of usage and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
nz committed Feb 27, 2025
1 parent 3be4c67 commit ee9b17a
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,52 @@
# Errgonomic

TODO: Delete this and the text below, and describe your gem
Errgonomic provides some lightweight, opinionated ergonomics for error handling in Ruby. These semantics are a blend of Rails `present?` conventions, and Rust `Option` and `Result` type combinators. Without going full Option and Result. Probably.

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/errgonomic`. To experiment with that code, run `bin/console` for an interactive prompt.
The `present_or` method takes what you might ordinarily write as `foo || default` with a possible nil or falsey value, and brings that to any other object that may be `blank?`.

```ruby
nil.present_or("default")
# => "default"

[].present_or(["default"])
# => ["default"]
```

We don't have static type checking here in Ruby, so the library is also _annoyingly_ pedantic about matching classes for the supplied default value.

```ruby
[].present_or("uh-oh")
# => Type mismatch: default value is a String but original was a Array (Errgonomic::TypeMismatchError)
```

When constructing that fallback object may be expensive, you can provide a block instead:

```ruby
[].present_or_else { ["default"] }
# => ["default"]
```

And when all else fails, you can control the failure, by raising an exception for blank objects. This can be preferable to sending a blank object to some other downstream code that may be expecting a value, causing an ambiguous failure.

```ruby
irb(main):007> [].present_or_raise("foo")
# => foo (Errgonomic::NotPresentError)
```

## 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.
TODO: Replace `errgonomic` 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.

Install the gem and add to the application's Gemfile by executing:

```bash
bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
bundle add errgonomic
```

If bundler is not being used to manage dependencies, install the gem by executing:

```bash
gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
gem install errgonomic
```

## Usage
Expand All @@ -32,7 +61,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/nz/errgonomic. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/nz/errgonomic/blob/main/CODE_OF_CONDUCT.md).
Bug reports and pull requests are welcome on GitHub at https://github.com/omc/errgonomic. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/nz/errgonomic/blob/main/CODE_OF_CONDUCT.md).

## License

Expand Down

0 comments on commit ee9b17a

Please sign in to comment.