Trying to come up with a nice DSL, but having some problems


#1

I have three models in this small game I’m working on - the Game,
Players, and Turns. A Player should be able to rescue another Player
on any given turn. So it looks like this:

class Game
has_many :players
has_many :turns

def rescue_player§
t = turns.last
t.rescued = p
t.save
end
end

class Turn
belongs_to :rescued, :foreign_key => ‘rescued_id’, :class_name =>
‘Player’
end

class Player
belongs_to :game

def rescue_player§
game.rescue_player p
end
end

So what I want to do is be able to create a game object and do
something like this:

g = create_game # factory method in test_helper that just creates a
new random game
g.players.first.rescue_player g.players.last
assert_equal g.players.last, g.turns.last.rescued

Now if I just do that, the data gets saved in the database, but the
assert fails. This is because g != g.players.last.game. I can do a
g.reload right before the assert and then it works fine. I’m just not
quite sure what I need to do, because I think it’s pretty lame to have
to reload every time I do something like this. Also when I’m actually
using the code I won’t remember to call reload, as it’s just not
really a natural thing to do. Can anyone give me some advice on how
to create the DSL I want, without resorting to adding ugliness like
random reload calls?

Pat