Skip to content

Instantly share code, notes, and snippets.

@dsisnero
Forked from dkubb/gist:189923
Created May 12, 2010 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsisnero/399097 to your computer and use it in GitHub Desktop.
Save dsisnero/399097 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