From 42a616648e1888ac0f4616057f802d6118d6d568 Mon Sep 17 00:00:00 2001 From: Brian J Ball Date: Mon, 13 Jun 2011 13:54:10 -0400 Subject: [PATCH] Added some database awareness to rooms --- src/room.erl | 14 +++++++++++++- test/room_tests.erl | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/src/room.erl b/src/room.erl index 2c13e10..6f93c75 100644 --- a/src/room.erl +++ b/src/room.erl @@ -1,14 +1,18 @@ -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], []). @@ -16,6 +20,14 @@ start_room(Key,Name,Description,Exits) -> 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}). diff --git a/test/room_tests.erl b/test/room_tests.erl index 121b5af..ff77216 100644 --- a/test/room_tests.erl +++ b/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.