Skip to content

Instantly share code, notes, and snippets.

@jacobat
Created July 3, 2014 20:39
Show Gist options
  • Save jacobat/4b88e3b1bcbacd9f9eeb to your computer and use it in GitHub Desktop.
Save jacobat/4b88e3b1bcbacd9f9eeb to your computer and use it in GitHub Desktop.
Hexagonal Ruby controllers
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