Commits

Kaspar Schiess committed 257d523

Transformation for top and bottom

  • Participants
  • Parent commits 4d2d260

Comments (0)

Files changed (2)

lib/par/result.rb

       def bottom?
         true
       end
-      def as obj
+      def as obj=nil, &block
         self
       end
     end
       def bottom?
         false
       end
-      def as obj
-        Result.new(obj)
+      def as obj=nil, &block
+        unless block_given?
+          return Result.new(obj)
+        end
+        return Result.new(block.call(self))
       end
     end
   end

spec/lib/par/result_spec.rb

     it "swallows .as(obj)" do
       bottom.as('something').should == bottom
     end 
+    it "swallows .as { ... }" do
+      bottom.as { fail }.should == bottom
+    end 
   end
   context '.top' do
     let(:any) { result('any') }
     it 'allows turning into an object' do
       top.as('something').should == 'something'
     end
+    it "allows transformation into an object" do
+      top.as { |o| 'obj' }.should == 'obj'
+    end 
   end
 end