forked from SnareHanger/Trade-Them
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.rb
50 lines (37 loc) · 1.15 KB
/
models.rb
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
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:dbfile => "db/tradethem.sqlite3"
)
class Player < ActiveRecord::Base
has_many :portfolio_items
has_many :stocks, :through => :portfolio_items
validates_presence_of :username, :cash
validates_numericality_of :cash
end
class Stock < ActiveRecord::Base
validates_presence_of :symbol, :price
validates_numericality_of :price
end
class PortfolioItem < ActiveRecord::Base
validates_presence_of :player_id, :stock_id, :purchase_price, :quantity
end
class Transaction < ActiveRecord::Base
belongs_to :stock
belongs_to :buyer, :class => 'Player'
belongs_to :seller, :class => 'Player'
validates_presence_of :price, :quantity
scope :executed, where(:executed => true)
scope :not_executed, where(:executed => false)
scope :active, where("expiration_date < ?", Time.now)
def execute!
self.executed = true
self.save!
end
end
class BuyTransaction < Transaction; end
class SellTransaction < Transaction; end
#Player id, cash
#PortfolioItem player_id, stock_id, purchase_price, quantity
#Stock code price total_shares
#Stock.find(:code => "foo")