Skip to content

Instantly share code, notes, and snippets.

@reactormonk
Forked from dkubb/gist:189923
Created September 20, 2009 21:19
Show Gist options
  • Save reactormonk/189931 to your computer and use it in GitHub Desktop.
Save reactormonk/189931 to your computer and use it in GitHub Desktop.
class Game
include DataMapper::Resource
#
# Properties
#
property :id, Serial
#
# Associations
#
has n, :game_memberships
has n, :players, :through => :game_memberships
end
class CaptainGame < Game
has n, :captain_memberships, 'GameMembership', :child_key => [ :game_id ], :captain => true
has 2, :captains, :through => :captain_memberships, :via => :players
end
class GameMembership
include DataMapper::Resource
#
# Properties
#
property :id, Serial
property :captain, Boolean, :nullable => false, :default => false
#
# Associations
#
belongs_to :game
belongs_to :player
end
class Player
include DataMapper::Resource
#
# Properties
#
property :id, Serial
#
# Associations
#
has n, :game_memberships
has n, :games, :through => :game_memberships
end
DataMapper.setup(:default, 'sqlite3::memory:')
DataMapper.auto_migrate!
require 'dm-sweatshop'
Player.fix {{ }}
Game.fix {{ }}
require 'bacon'
describe 'CaptainGame' do
it 'should assign 2 captains' do
players = 5.of {Player.gen}
game = CaptainGame.gen(:captains => players[0..1])
game.captains.should.equal players[0..1]
game.players.should.equal players[0..1]
game.players << players[2..4]
game.captains.should.equal players[0..1]
game.players.should.equal players
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment