Class: MailChess::Player

Inherits:
Object
  • Object
show all
Defined in:
lib/mail_chess/player.rb

Overview

Represents a player with an email account.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db, player_id) ⇒ Player

Create a new Player object.

Arguments:

db: Sequel database object
player_id: (Integer)


23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mail_chess/player.rb', line 23

def initialize db, player_id
  @db = db
  
  Player.create_tables(@db) if Player.need_to_create_tables?(@db)

  @db_entry = @db[:players].filter(:id => player_id).first

  @id = @db_entry[:id].to_i
  @name = @db_entry[:name]
  @email = @db_entry[:email]
end

Instance Attribute Details

#emailObject (readonly)

Player email address.



16
17
18
# File 'lib/mail_chess/player.rb', line 16

def email
  @email
end

#idObject (readonly)

Player ID in the database.



10
11
12
# File 'lib/mail_chess/player.rb', line 10

def id
  @id
end

#nameObject

Player name.



13
14
15
# File 'lib/mail_chess/player.rb', line 13

def name
  @name
end

Class Method Details

.create_tables(db) ⇒ Object

Create tables.



43
44
45
46
47
48
49
# File 'lib/mail_chess/player.rb', line 43

def self.create_tables db
  db.create_table? :players do
    primary_key :id
    String :name
    String :email
  end
end

.load!(db, filter, value) ⇒ Object

Load player from database.

Arguments:

db: Sequel database object
filter: (Symbol) :id or :email
value: filters value

Returns: (Player)

Exceptions:

(ArgumentError) if filter is invalid
(PlayerError) if player not exists

Raises:

  • (ArgumentError)


79
80
81
82
83
84
85
86
87
# File 'lib/mail_chess/player.rb', line 79

def self.load! db, filter, value
  raise ArgumentError unless [:id, :email].index(filter)
  dataset = db[:players].filter(filter => value)
  raise PlayerError('player not exists') unless dataset.all.size == 1

  id = dataset.first[:id]

  return Player.new db, id
end

.need_to_create_tables?(db) ⇒ Boolean

Checks if needed to create tables.

Returns: (Boolean)

Returns:

  • (Boolean)


38
39
40
# File 'lib/mail_chess/player.rb', line 38

def self.need_to_create_tables? db
  return (db.tables + [:players]).uniq.size != db.tables.size
end

.new!(db, email, name = "") ⇒ Object

Create a new player object and save it to the database.

Arguments:

db: Sequel database object
email: (String) players email address
name: (String) players name

Returns: (Player)



59
60
61
62
63
64
65
# File 'lib/mail_chess/player.rb', line 59

def self.new! db, email, name = ""
  Player.create_tables(db) if Player.need_to_create_tables?(db)

  id = db[:players].insert(:name => name, :email => email)

  return Player.new db, id
end

Instance Method Details

#game(reference) ⇒ Object

Find player’s game by a reference string.

Arguments:

reference: (String) game reference string

Returns: (Game) or nil

Exceptions:

(PlayerError) if initiator is invalid


98
99
100
101
102
103
104
105
106
107
108
# File 'lib/mail_chess/player.rb', line 98

def game reference
  d = @db[:pairs]
    .join(:games, :id => :game_id)
    .filter(:reference => reference, :player_id => self.id)

  if d.all.empty?
    raise PlayerError('invalid initiator')
  else
    Game.load! @db, :reference, reference, d.first[:color].to_sym
  end
end

#save!Object

Save player – name only



111
112
113
# File 'lib/mail_chess/player.rb', line 111

def save!
  @db[:players].filter(:id => @id).update(:name => @name)
end