Class: BaseballPlayer

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#birth_yearObject

Returns the value of attribute birth_year.



2
3
4
# File 'lib/bb_analytics/models/baseball_player.rb', line 2

def birth_year
  @birth_year
end

#external_idObject

Returns the value of attribute external_id.



2
3
4
# File 'lib/bb_analytics/models/baseball_player.rb', line 2

def external_id
  @external_id
end

#first_nameObject

Returns the value of attribute first_name.



2
3
4
# File 'lib/bb_analytics/models/baseball_player.rb', line 2

def first_name
  @first_name
end

#last_nameObject

Returns the value of attribute last_name.



2
3
4
# File 'lib/bb_analytics/models/baseball_player.rb', line 2

def last_name
  @last_name
end

Class Method Details

.find_by_external_id(external_id) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/bb_analytics/models/baseball_player.rb', line 34

def self.find_by_external_id external_id
  player = BaseballPlayer.new
  DataStore.instance.db.execute("select * from baseball_players where external_id = ?",
                                                                  [external_id]) do |row|
    player.external_id = row[0]
    player.birth_year  = row[1]
    player.first_name  = row[2]
    player.last_name   = row[3]
  end
  player
end

Instance Method Details

#reloadObject



24
25
26
27
28
29
30
31
32
# File 'lib/bb_analytics/models/baseball_player.rb', line 24

def reload
  DataStore.instance.db.execute("select * from baseball_players where external_id = ?",
                                                            [self.external_id]) do |row|
    self.birth_year  = row[1]
    self.first_name  = row[2]
    self.last_name   = row[3]
  end
  self
end

#saveObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/bb_analytics/models/baseball_player.rb', line 4

def save
  # first see if we can update an existing record
  DataStore.instance.db.execute("select * from baseball_players where external_id = ?",
                                                            [self.external_id]) do |row|
    DataStore.instance.db.execute "update baseball_players set birth_year = ?, first_name = ?, last_name = ? where external_id = ?",
        [(self.birth_year.present? ? self.birth_year : row[1]),
        (self.first_name.present? ? self.first_name : row[2]),
        (self.last_name.present? ? self.last_name : row[3]),
        self.external_id]
    return
  end

  # if not, just insert
  DataStore.instance.db.execute "insert into baseball_players values (?,?,?,?)",
                                                              [self.external_id,
                                                                self.birth_year,
                                                                self.first_name,
                                                                self.last_name]
end