Permalink
Browse files

added an operator + to Either (combine validations!)

  • Loading branch information...
1 parent 0a0a2de commit 27cda8b6fa73a553e3b753b35f3c2ace067cbae7 @ms-ati committed Apr 27, 2012
Showing with 15 additions and 0 deletions.
  1. +10 −0 lib/rumonade/either.rb
  2. +5 −0 test/either_test.rb
View
@@ -43,6 +43,16 @@ def left
def right
RightProjection.new(self)
end
+
+ # @param [Either] other the either to combine with
+ # @return [Either] Returns a +Left+ with all Left values (if any), otherwise a +Right+ with all Right values
+ def +(other)
+ if [self, other].any?(&:left?)
+ Left([self, other].map { |either| either.left.to_opt }.flatten)
+ else
+ Right([self, other].map { |either| either.right.to_opt }.flatten)
+ end
+ end
end
# The left side of the disjoint union, as opposed to the Right side.
View
@@ -159,4 +159,9 @@ def test_to_s_for_left_and_right_and_their_projections
assert_equal "RightProjection(Left(error))", Left("error").right.to_s
assert_equal "LeftProjection(Right(42))", Right(42).left.to_s
end
+
+ def test_plus_for_left_and_right
+ assert_equal Left(["bad", "worse"]), Left("bad") + Right(:good) + Left("worse") + Right(:good)
+ assert_equal Right([:good, :better]), Right(:good) + Right(:better)
+ end
end

0 comments on commit 27cda8b

Please sign in to comment.