Git Product home page Git Product logo

resultt's Introduction

Resultt

A simple library to wrap execution result in smart success or error objects.

success_result = Result do
  1 + 1
end
# => #<Resultt::Success:0x0000000098c718 @value=2>

success_result.success? # true
success_result.value # 2
success_result.error? # false

error_result = Result do
  raise StandardError, 'error result'
end
# => #<Resultt::Error:0x000000007c5498 @error=#<StandardError: error result>>

error_result.error? # true
error_result.error # #<StandardError: error result>
result = Result do
  # do something
end

case result
when Resultt::Success then 'it is a success!'
when Resultt::Error then 'oh no! error!'
end

Installation

Add this line to your application's Gemfile:

gem 'resultt'

And then execute:

$ bundle

Or install it yourself as:

$ gem install resultt

Usage

extend Resultt in your class and you're good to go!

class MyClass
  extend Resultt

  def foo
    Result do
      # do something
    end
  end
end

map and map_error

it is possible to map on values and errors in Resultt objects using map and map_error respectively:

result = Result { 1 + 1 }
# => #<Resultt::Success:0x000000007502d8 @value=2>
mapped_result = result.map { |value| value + 1 }
# => #<Resultt::Success:0x000000007502d8 @value=3>

result = Result { raise StandardError, 'error' }
# => #<Resultt::Error:0x00000000bc8d88 @error=#<StandardError: error>>
mapped_result = result.map_error { |error| error.class.to_s }
# => #<Resultt::Error:0x00000000be6568 @error="StandardError">

Success and Error

it is also possible to wrap values or errors in Resultt::Success or Resultt::Error objects directly:

Success('ok')
# => #<Resultt::Success:0x0000000076b718 @value="ok">
Error('err')
# => #<Resultt::Error:0x00000000764a08 @error="err">

Nil values

Result will return Resultt::NilValueError if the block passed to it evaluates to nil. This is to avoid having success results with nil values.

Result do
  nil
end
# => #<Resultt::Error:0x00000000afa000 @error=#<Resultt::NilValueError: Result returned a nil value>>

Development

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.

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 tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/amrrbakry/resultt. 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.

Code of Conduct

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

resultt's People

Contributors

amrrbakry avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

tarek-elsayyad

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.