Overview

HTTPS SSH

Viator

Sometimes exceptions are not what I want. Sometimes they are ugly. Sometimes they are overkill. Throwing exceptions seems wrong for correct and expected behaviour. Sometimes I want:

result = User.create('pogs_for_life', 'password', 'pogsandhorses@geocities.com')

if result.success?
  do_something(result.value)
else
  result.errors.each { |e| do_something_else(e) }
end

This can be especially nice when you are using the builder pattern. Other solutions I have seen are overly-complicated, often using monads, which I do not think fit Ruby very nicely. I find myself rewriting variations of this again and again, so I have made a gem.

Viator is extremely simple, suspiciously so.

Installation

In your Gemfile:

gem 'viator'

or on the console:

$ gem install viator

Usage

eh = Viator.new # create a new error handler
eh.success?     # true
eh.failure?     # false
eh.count        # 0
eh.errors       # []

eh.report 'argument invalid!' 
eh.success?     # false
eh.count        # 1
eh.errors       # ['argument invalid!']

eh.reset                                 
eh.success?     # true

eh.value = 'arbitrary data'
eh.value        # returns 'nil' if eh.failure?

# Less Important

eh = Viator.new(hide_value: false)
eh.value = 'berries'
eh.report 'There has been an error'
eh.value        # 'berries'
eh.hide_value = true
eh.value        # nil

Viator.new(value: 'foo', hide_value: true)

Contributing

Bug reports and pull requests are welcome on Bitbucket at https://bitbucket.org/surdegg/viator. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

This project was created by Surd Egg. News and updates can be found here.

Code of Conduct

Everyone interacting in the Viator project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.