Browse files

Added some database awareness to rooms

  • Loading branch information...
1 parent d24f41f commit 42a616648e1888ac0f4616057f802d6118d6d568 @Ball committed Jun 13, 2011
Showing with 57 additions and 1 deletion.
  1. +13 −1 src/room.erl
  2. +44 −0 test/room_tests.erl
View
14 src/room.erl
@@ -1,21 +1,33 @@
-module(room).
-include("src/records.hrl").
+-include_lib("stdlib/include/qlc.hrl").
-behavior(gen_server).
%% API
--export([start_room/4,start_room/3, add_to_room/2, take_from_room/2, direction/2, describe/1]).
+-export([start_from_db/0, start_from_db/1, start_room/4,start_room/3, add_to_room/2, take_from_room/2, direction/2, describe/1]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
+start_room(Record=#room{key=Key}) ->
+ gen_server:start_link({global,Key}, ?MODULE, [Record], []).
+
start_room(Key,Name,Description,Exits) ->
Room = #room{key=Key,name=Name,description=Description,exits=Exits},
gen_server:start_link({global,Key}, ?MODULE,[Room], []).
start_room(Key,Name,Description) ->
Room = #room{key=Key,name=Name,description=Description},
gen_server:start_link({global,Key}, ?MODULE, [Room], []).
+start_from_db() ->
+ {atomic, Rows} = mnesia:transaction(fun() ->
+ qlc:eval( qlc:q([ X || X <- mnesia:table(room)])) end),
+ lists:map( fun (A) -> start_room(A) end, Rows).
+start_from_db(Key) ->
+ {atomic, [Row]}=mnesia:transaction(fun() ->
+ mnesia:read({room,Key}) end),
+ start_room(Row).
add_to_room(RoomName,Item) ->
gen_server:call({global,RoomName}, {add_item, Item}).
View
44 test/room_tests.erl
@@ -1,4 +1,5 @@
-module(room_tests).
+-include("../src/records.hrl").
-include("tests.hrl").
room_test_() -> [
@@ -35,5 +36,48 @@ room_test_() -> [
])
].
+room_database_test_() -> [
+ ?Describe("run a room from the database",
+ [?It("should load a stable from the database",
+ fun insert_stable/0, fun delete_stable/1,
+ begin
+ room:start_from_db(stable),
+ {ok,Description} = room:describe(stable),
+ ?assertEqual("A stable from the database",Description)
+ end)
+ ]),
+ ?Describe("run all rooms in the database",
+ [?It("should start multiple rooms",
+ fun insert_stable/0, fun delete_stable/1,
+ begin
+ room:start_from_db(),
+ {ok,Description1} = room:describe(stable),
+ {ok,Description2} = room:describe(yard),
+ ?assertEqual("A stable from the database", Description1),
+ ?assertEqual("A yard from the database", Description2)
+ end)
+ ])
+].
+
+insert_stable() ->
+ mnesia:transaction(fun() ->
+ mnesia:write(
+ #room{key=yard,
+ name="The horse yard",
+ description="A yard from the database"}
+ ),
+ mnesia:write(
+ #room{key=stable,
+ name="The king's stable",
+ description="A stable from the database"}
+ )
+ end).
+delete_stable(_Pid) ->
+ gen_server:cast({global, stable}, stop),
+ mnesia:transaction(fun() ->
+ mnesia:delete(room, yard),
+ mnesia:delete(room, stable) end),
+ true.
+
setup()-> io:format(""),stubs:fake_rooms().
cleanup(_Pid) -> stubs:stop_fake_rooms(), true.

0 comments on commit 42a6166

Please sign in to comment.