dkubb (owner)

Fork Of

Forks

Revisions

gist: 189923 Download_button fork
public
Public Clone URL: git://gist.github.com/189923.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/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