Created
February 25, 2015 15:27
-
-
Save kevbuchanan/65a7297f5bb78a4bb114 to your computer and use it in GitHub Desktop.
Result Object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Result | |
def self.status(status, result) | |
new(status, result) | |
end | |
def initialize(status, result) | |
@status = status | |
@result = result | |
end | |
def on_status(status, &block) | |
if @status == status | |
Result.status(@status, block.call(result)) | |
else | |
Result.status(@status, result) | |
end | |
end | |
end | |
result = Result.status(:not_found, nil) | |
title = result.on_status(:success) do |post| | |
post.title | |
end.on_status(:not_found) do | |
"Title not found" | |
end.result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment