Skip to content

Instantly share code, notes, and snippets.

@dkubb
Forked from reactormonk/gist:189919
Created September 20, 2009 21:00
Show Gist options
  • Save dkubb/189923 to your computer and use it in GitHub Desktop.
Save dkubb/189923 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby -Ku
require 'rubygems'
require 'dm-core'
require 'dm-sweatshop'
require 'bacon'
#DataMapper::Logger.new($stdout, :debug)
DataMapper.setup(:default, 'sqlite3::memory:')
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, 'Player', :through => :captain_memberships, :via => :player
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.auto_migrate!
Player.fix {{ }}
Game.fix {{ }}
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.concat(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