Project

maybe2

0.0
No commit activity in last 3 years
No release in over 3 years
Be able to chain method calls without raising a NoMethodError if there is a nil somewhere along the way.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.10
~> 0.10
~> 10.4
~> 3.3
~> 0.32
~> 0.10
~> 0.8
 Project Readme

Maybe2 Build Status Code Climate Gem Version

Wrap a value into a Maybe monad (or Optional type). Since monads are chainable and every value is wrapped in one, things don't blow up with NoMethodError on nils.

Useful for retrieving something in deeply nested structures where nils could be produced along the way.

Usage

maybe = Maybe.new('Hello world').upcase.reverse
maybe.class  # => Maybe
maybe.unwrap # => 'Hello world'

maybe = Maybe.new(nil).upcase.reverse
maybe.class  # => Maybe
maybe.unwrap # => nil

maybe = Maybe.new('Hello world').make_nil.upcase.reverse
maybe.class  # => Maybe
maybe.unwrap # => nil

JSON example evaluation to a string:

response = {
  data: {
    item: {
      name: 'Hello!'
    }
  }
}

Maybe.new(good_response)[:data][:item][:name].upcase.unwrap # => 'HELLO!'

JSON example evaluating to nil:

response = {
  data: {}
}

Maybe.new(good_response)[:data][:item][:name].upcase.unwrap # => nil

Installation

Add this line to your application's Gemfile:

gem 'maybe2'

And then execute:

$ bundle

Or install it yourself as:

$ gem install maybe2

Development

After checking out the repo, run bin/setup to install dependencies. Then, run bin/console for an interactive prompt that will allow you to experiment.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/ollie/maybe2.

License

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