Created
July 3, 2014 20:39
-
-
Save jacobat/4b88e3b1bcbacd9f9eeb to your computer and use it in GitHub Desktop.
Hexagonal Ruby controllers
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 success(&blk) | |
@success = blk | |
end | |
def succeeded | |
@success.call | |
end | |
def failure(&blk) | |
@failure = blk | |
end | |
def failed | |
@failure.call | |
end | |
end | |
class CreateCommand | |
def apply | |
result = Result.new | |
yield result | |
if rand(2) == 0 | |
result.succeeded | |
else | |
result.failed | |
end | |
end | |
end | |
class Controller | |
def create | |
CreateCommand.new.apply do |on| | |
on.success do | |
puts "Success" | |
end | |
on.failure do | |
puts "Fail" | |
end | |
end | |
end | |
end | |
Controller.new.create |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment