Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass method call directly to inner value #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
16 changes: 16 additions & 0 deletions lib/possibly.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ def ==(other)

# Represents a non-empty value
class Some < Maybe

class SomeInnerValue

def initialize(value)
@value = value
end

def method_missing(method_sym, *args, &block)
Maybe(@value.send(method_sym, *args, &block))
end
end

def initialize(value)
@value = value
end
Expand Down Expand Up @@ -54,6 +66,10 @@ def method_missing(method_sym, *args, &block)
map { |value| value.send(method_sym, *args, &block) }
end

def inner
SomeInnerValue.new(@value)
end

private

def __enumerable_value
Expand Down
8 changes: 8 additions & 0 deletions spec/spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,12 @@ 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

describe "inner" do
it "forwards all (also Enumerable) methods" do
expect(Some(["first", "second", "third"]).inner.first.upcase.or_else { false }).to eql("FIRST")
expect(Some(["first", "second", "third"]).inner.map(&:upcase).or_else { false }).to eql(["FIRST", "SECOND", "THIRD"])
expect(Some([]).inner.first.upcase.or_else { "NONE" }).to eql("NONE")
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add test for None (it will probably break)

end
end
end