Class: Game

Inherits:
Object
  • Object
show all
Defined in:
lib/models/game.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGame

Returns a new instance of Game.



24
25
26
27
28
29
30
# File 'lib/models/game.rb', line 24

def initialize
    @id = nil
    @player_name = nil
    @wins = 0
    @draws = 0
    @score = 0
end

Instance Attribute Details

#drawsObject

Returns the value of attribute draws.



22
23
24
# File 'lib/models/game.rb', line 22

def draws
  @draws
end

#idObject

Returns the value of attribute id.



22
23
24
# File 'lib/models/game.rb', line 22

def id
  @id
end

#player_nameObject

Returns the value of attribute player_name.



22
23
24
# File 'lib/models/game.rb', line 22

def player_name
  @player_name
end

#scoreObject

Returns the value of attribute score.



22
23
24
# File 'lib/models/game.rb', line 22

def score
  @score
end

#winsObject

Returns the value of attribute wins.



22
23
24
# File 'lib/models/game.rb', line 22

def wins
  @wins
end

Class Method Details

.mapObject



7
8
9
# File 'lib/models/game.rb', line 7

def map
    @games.map { |game_ary| yield game_ary }
end

.next_idObject



11
12
13
# File 'lib/models/game.rb', line 11

def next_id
    @games.length + 1
end

.save(game) ⇒ Object



15
16
17
18
19
# File 'lib/models/game.rb', line 15

def save(game)
    game.id = next_id
    @games << game
    File.open("lib/games.yml", "w") { |file| file.write(@games.to_yaml) }
end

Instance Method Details

#save!Object



32
33
34
# File 'lib/models/game.rb', line 32

def save!
    self.class.save(self)
end