Class: Stock

Inherits:
Object
  • Object
show all
Defined in:
lib/portfolio_maker/stock.rb

Constant Summary collapse

@@all =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#equityObject

Returns the value of attribute equity.



3
4
5
# File 'lib/portfolio_maker/stock.rb', line 3

def equity
  @equity
end

#market_capObject

Returns the value of attribute market_cap.



3
4
5
# File 'lib/portfolio_maker/stock.rb', line 3

def market_cap
  @market_cap
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/portfolio_maker/stock.rb', line 3

def name
  @name
end

#priceObject

Returns the value of attribute price.



3
4
5
# File 'lib/portfolio_maker/stock.rb', line 3

def price
  @price
end

#quantityObject

Returns the value of attribute quantity.



3
4
5
# File 'lib/portfolio_maker/stock.rb', line 3

def quantity
  @quantity
end

#tickerObject

Returns the value of attribute ticker.



3
4
5
# File 'lib/portfolio_maker/stock.rb', line 3

def ticker
  @ticker
end

Class Method Details

.allObject



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_tickersObject



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_stocksObject



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_equityObject



43
44
45
46
47
# File 'lib/portfolio_maker/stock.rb', line 43

def curr_equity
  
  @equity = @quantity * (self.price.to_f)

end

#displayObject



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_stockObject



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_quantityObject



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_priceObject



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