Skip to content

Instantly share code, notes, and snippets.

@ecomba
Created July 11, 2013 14:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ecomba/5975778 to your computer and use it in GitHub Desktop.
Save ecomba/5975778 to your computer and use it in GitHub Desktop.
A little gist showing how you could bind an app with an adapter to a web controller.
class Board
def add_ship ship
ship
end
def shoot(coordinates, player)
raise "Not your turn" if player == 2
'BANG'
end
end
class GameInteractor
def initialize
@board = Board.new
end
def add_ship params, listener
ship = @board.add_ship params[:ship][:name]
listener.ship_added(ship)
end
def shoot coordinates, player, listener
begin
@board.shoot(coordinates, player)
listener.shot_at coordinates
rescue Exception => e
listener.not_his_turn player, e.message
end
end
end
class Sinatra
def initialize
@game = GameInteractor.new
end
def add_ship params
@game.add_ship(params, self)
end
def shoot coordinates, player
@game.shoot coordinates, player, self
end
def ship_added ship
"That's awesome, thank you!"
end
def shot_at coordinates
"You have shot at #{coordinates}"
end
def not_his_turn player, message
"Hey #{player}! #{message}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment