Class: Stock
- Inherits:
-
Object
- Object
- Stock
- Defined in:
- lib/portfolio_maker/stock.rb
Constant Summary collapse
- @@all =
[]
Instance Attribute Summary collapse
-
#equity ⇒ Object
Returns the value of attribute equity.
-
#market_cap ⇒ Object
Returns the value of attribute market_cap.
-
#name ⇒ Object
Returns the value of attribute name.
-
#price ⇒ Object
Returns the value of attribute price.
-
#quantity ⇒ Object
Returns the value of attribute quantity.
-
#ticker ⇒ Object
Returns the value of attribute ticker.
Class Method Summary collapse
- .all ⇒ Object
- .create_or_buy_more(stock_hash, amount) ⇒ Object
- .display_tickers ⇒ Object
- .find_stock_with_ticker(ticker) ⇒ Object
- .sell(ticker, amount) ⇒ Object
- .update_stocks ⇒ Object
Instance Method Summary collapse
- #curr_equity ⇒ Object
- #display ⇒ Object
-
#initialize(stock_hash) ⇒ Stock
constructor
A new instance of Stock.
- #make_hash_from_stock ⇒ Object
- #update_quantity ⇒ Object
- #update_stock_price ⇒ Object
Constructor Details
#initialize(stock_hash) ⇒ Stock
Returns a new instance of Stock.
7 8 9 10 11 12 |
# File 'lib/portfolio_maker/stock.rb', line 7 def initialize(stock_hash) stock_hash.each {|key, value| self.send(("#{key}="), value)} @quantity = self.update_quantity end |
Instance Attribute Details
#equity ⇒ Object
Returns the value of attribute equity.
3 4 5 |
# File 'lib/portfolio_maker/stock.rb', line 3 def equity @equity end |
#market_cap ⇒ Object
Returns the value of attribute market_cap.
3 4 5 |
# File 'lib/portfolio_maker/stock.rb', line 3 def market_cap @market_cap end |
#name ⇒ Object
Returns the value of attribute name.
3 4 5 |
# File 'lib/portfolio_maker/stock.rb', line 3 def name @name end |
#price ⇒ Object
Returns the value of attribute price.
3 4 5 |
# File 'lib/portfolio_maker/stock.rb', line 3 def price @price end |
#quantity ⇒ Object
Returns the value of attribute quantity.
3 4 5 |
# File 'lib/portfolio_maker/stock.rb', line 3 def quantity @quantity end |
#ticker ⇒ Object
Returns the value of attribute ticker.
3 4 5 |
# File 'lib/portfolio_maker/stock.rb', line 3 def ticker @ticker end |
Class Method Details
.all ⇒ Object
114 115 116 |
# File 'lib/portfolio_maker/stock.rb', line 114 def self.all @@all end |
.create_or_buy_more(stock_hash, amount) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/portfolio_maker/stock.rb', line 101 def self.create_or_buy_more(stock_hash, amount) if (!self.find_stock_with_ticker(stock_hash[:ticker])) stock_hash[:equity] = amount new_stock = self.new(stock_hash) @@all << new_stock else updating_stock = self.find_stock_with_ticker(stock_hash[:ticker]) updating_stock.update_stock_price updating_stock.quantity += (amount/updating_stock.price) end end |
.display_tickers ⇒ Object
83 84 85 86 87 |
# File 'lib/portfolio_maker/stock.rb', line 83 def self.display_tickers @@all.each {|stock| print "|#{stock.ticker}"} puts "|" end |
.find_stock_with_ticker(ticker) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/portfolio_maker/stock.rb', line 63 def self.find_stock_with_ticker(ticker) stock = @@all.select{|stock|stock.ticker == ticker} stock == [] ? nil : stock[0] end |
.sell(ticker, amount) ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/portfolio_maker/stock.rb', line 14 def self.sell(ticker, amount) self.find_stock_with_ticker(ticker).equity -= amount if (self.find_stock_with_ticker(ticker).equity == 0) @@all.delete(self.find_stock_with_ticker(ticker)) else self.find_stock_with_ticker(ticker).update_quantity end end |
.update_stocks ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/portfolio_maker/stock.rb', line 89 def self.update_stocks #binding.pry price_update = 0 @@all.each do |stock| curr_price = stock.price stock.update_stock_price price_update = stock.price - price_update end price_update end |
Instance Method Details
#curr_equity ⇒ Object
43 44 45 46 47 |
# File 'lib/portfolio_maker/stock.rb', line 43 def curr_equity @equity = @quantity * (self.price.to_f) end |
#display ⇒ Object
77 78 79 80 81 |
# File 'lib/portfolio_maker/stock.rb', line 77 def display puts "#{@name} (#{@ticker}): stocks: #{@quantity} shares, equity: $#{self.curr_equity}" end |
#make_hash_from_stock ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/portfolio_maker/stock.rb', line 25 def make_hash_from_stock stock_info = {} stock_info[:name] = self.name stock_info[:price] = self.price stock_info[:market_cap] = self.market_cap stock_info end |
#update_quantity ⇒ Object
37 38 39 40 41 |
# File 'lib/portfolio_maker/stock.rb', line 37 def update_quantity @quantity = @equity/(self.price.to_f) end |
#update_stock_price ⇒ Object
49 50 51 52 53 54 |
# File 'lib/portfolio_maker/stock.rb', line 49 def update_stock_price self.price = Scraper.scrape_stock_page("https://finance.yahoo.com/quote/" + self.ticker)[:price] self.curr_equity end |