class Stock

Attributes

equity[RW]
market_cap[RW]
name[RW]
price[RW]
quantity[RW]
ticker[RW]

Public Class Methods

all() click to toggle source
# File lib/portfolio_maker/stock.rb, line 114
def self.all
  @@all
end
create_or_buy_more(stock_hash, amount) click to toggle source
# 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() click to toggle source
# 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) click to toggle source
# 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
new(stock_hash) click to toggle source
# 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
sell(ticker, amount) click to toggle source
# 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() click to toggle source
# 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

Public Instance Methods

curr_equity() click to toggle source
# File lib/portfolio_maker/stock.rb, line 43
def curr_equity
  
  @equity = @quantity * (self.price.to_f)

end
display() click to toggle source
# 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() click to toggle source
# 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
price=(price) click to toggle source
# File lib/portfolio_maker/stock.rb, line 56
def price=(price)

  @price = price

end
update_quantity() click to toggle source
# File lib/portfolio_maker/stock.rb, line 37
def update_quantity

  @quantity = @equity/(self.price.to_f)

end
update_stock_price() click to toggle source
# 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