Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 32 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Maybe monad implementation for Ruby

```ruby
puts Maybe(User.find_by_id("123")).username.downcase.or_else { "N/A" }
puts Maybe(User.find_by_id("123")).username.downcase.get_or_else { "N/A" }

=> # puts downcased username if user "123" can be found, otherwise puts "N/A"
```
Expand All @@ -21,7 +21,7 @@ gem install possibly
```
require 'possibly'

first_name = Maybe(deep_hash)[:account][:profile][:first_name].or_else { "No first name available" }
first_name = Maybe(deep_hash)[:account][:profile][:first_name].get_or_else { "No first name available" }
```

## Documentation
Expand All @@ -38,14 +38,14 @@ Maybe(nil) => #<None:0x007ff7a852bd20>
Both `Some` and `None` implement four trivial methods: `is_some?`, `is_none?`, `get` and `or_else`

```ruby
Maybe("I'm a value").is_some? => true
Maybe("I'm a value").is_none? => false
Maybe(nil).is_some? => false
Maybe(nil).is_none? => true
Maybe("I'm a value").get => "I'm a value"
Maybe("I'm a value").or_else { "No value" } => "I'm a value"
Maybe(nil).get => RuntimeError: No such element
Maybe(nil).or_else { "No value" } => "No value"
Maybe("I'm a value").is_some? => true
Maybe("I'm a value").is_none? => false
Maybe(nil).is_some? => false
Maybe(nil).is_none? => true
Maybe("I'm a value").get => "I'm a value"
Maybe("I'm a value").get_or_else { "No value" } => "I'm a value"
Maybe(nil).get => RuntimeError: No such element
Maybe(nil).get_or_else { "No value" } => "No value"
```

In addition, `Some` and `None` implement `Enumerable`, so all methods available for `Enumerable` are available for `Some` and `None`:
Expand Down Expand Up @@ -116,6 +116,26 @@ when None
end
```

## or_else

`or_else` returns the current `Maybe` if it's a `Some`, but if it's a `None`, it returns the parameter that was given to it (which should be a `Maybe`).

Here's an example: Show "title", which is person's job title or degree if she doesn't have a job or "Unknown" if both are missing.

```ruby
maybe_person = Maybe(person)

title = maybe_person.job.title.or_else { maybe_person.degree }.get_or_else { "Unknown" }

title = if person && person.job && person.job.title.present?
person.job.title
elsif person && person.degree.present?
person.degree
else
"Unknown"
end
```

## Examples

Instead of using if-clauses to define whether a value is a `nil`, you can wrap the value with `Maybe()` and threat it the same way whether or not it is a `nil`
Expand All @@ -134,7 +154,7 @@ end
With Maybe():

```ruby
number_of_friends = Maybe(User.find_by_id(user_id)).friends.count.or_else { 0 }
number_of_friends = Maybe(User.find_by_id(user_id)).friends.count.get_or_else { 0 }
```

Same in HAML view, without Maybe():
Expand All @@ -147,7 +167,7 @@ Same in HAML view, without Maybe():
```

```haml
= Maybe(@user).friends.count.or_else { 0 }
= Maybe(@user).friends.count.get_or_else { 0 }
```

## Tests
Expand Down
12 changes: 10 additions & 2 deletions lib/possibly.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ def get
@value
end

def or_else(*)
def get_or_else(*)
@value
end

def or_else(*)
self
end

# rubocop:disable PredicateName
def is_some?
true
Expand Down Expand Up @@ -67,10 +71,14 @@ def get
fail 'No such element'
end

def or_else(els = nil)
def get_or_else(els = nil)
block_given? ? yield : els
end

def or_else(els = nil, &block)
block ? block.call : els
end

# rubocop:disable PredicateName
def is_some?
false
Expand Down
37 changes: 30 additions & 7 deletions spec/spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,40 @@ def test_case_when(case_value, match_value, non_match_value)
end
end

describe "get and or_else" do
describe "get and get_or_else" do
it "get" do
expect { None.get }.to raise_error
expect(Some(1).get).to eql(1)
end

it "or_else" do
expect(None().or_else(true)).to eql(true)
expect(None().or_else { false }).to eql(false)
expect(Some(1).or_else(2)).to eql(1)
expect(Some(1).or_else { 2 }).to eql(1)
it "get_or_else" do
expect(None().get_or_else(true)).to eql(true)
expect(None().get_or_else { false }).to eql(false)
expect(Some(1).get_or_else(2)).to eql(1)
expect(Some(1).get_or_else { 2 }).to eql(1)
end
end

describe "or_else" do
it "returns self if it's a Some" do
current = Maybe(true)
other = Maybe(true)

expect(current.or_else(other)).to equal current
end

it "returns other if it's a Some" do
current = Maybe(nil)
other = Maybe(true)

expect(current.or_else(other)).to equal other
end

it "takes also a block" do
current = Maybe(true)
other = Maybe(true)

expect(current.or_else { other }).to equal current
end
end

Expand All @@ -156,4 +179,4 @@ def test_case_when(case_value, match_value, non_match_value)
expect(Some([1, 2, 3]).map { |arr| arr.map { |v| v * v } }.get).to eql([1, 4, 9])
end
end
end
end