Class: BaseballPlayer
- Inherits:
-
Object
- Object
- BaseballPlayer
- Defined in:
- lib/bb_analytics/models/baseball_player.rb
Instance Attribute Summary collapse
-
#birth_year ⇒ Object
Returns the value of attribute birth_year.
-
#external_id ⇒ Object
Returns the value of attribute external_id.
-
#first_name ⇒ Object
Returns the value of attribute first_name.
-
#last_name ⇒ Object
Returns the value of attribute last_name.
Class Method Summary collapse
Instance Method Summary collapse
Instance Attribute Details
#birth_year ⇒ Object
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_id ⇒ Object
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_name ⇒ Object
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_name ⇒ Object
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
#reload ⇒ Object
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 |
#save ⇒ Object
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 |