Class: Portfolio::Commands

Inherits:
Object
  • Object
show all
Defined in:
lib/portfolio/commands.rb

Instance Method Summary collapse

Instance Method Details

#helpObject

Show usage information



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/portfolio/commands.rb', line 101

def help
  puts "Portfolio, v#{Portfolio::VERSION}. Commands:\n".bold.underline
  puts "Start watching a stock:".blue
  puts "  " + "--watch <stock>\n".underline
  puts "Stop watching a stock:".blue
  puts "  " + "--ignore <stock>\n".underline
  puts "Add investment to portfolio:".blue
  puts "  " + "--invest <stock> <amount bought> <price paid per stock> <total commission paid>\n".underline
  puts "Remove investment from portfolio:".blue
  puts "  " + "--sell <stock>\n".underline
end

#ignoreObject

Stop watching a stock



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/portfolio/commands.rb', line 43

def ignore
  if ARGV.length != 2
    return help()
  end
  
  stock_to_remove = ARGV[1].upcase
  watched_stocks = Store.instance.watched_stocks
  watched_stocks.delete stock_to_remove
  
  Store.instance.watched_stocks = watched_stocks
end

#investObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/portfolio/commands.rb', line 55

def invest
  if ARGV.length != 5
    return help()
  end
  
  # Get parameters:
  stock = ARGV[1]
  amount_bought = ARGV[2]
  price_paid = ARGV[3]
  commission_paid = ARGV[4]
  
  # Store new investment:
  portfolio = Store.instance.portfolio
  portfolio << {
    stock: stock,
    amount_bought: amount_bought,
    price_paid: price_paid,
    commission_paid: commission_paid,
  }
  Store.instance.portfolio = portfolio
  
  # Tell the user that the investment has been logged and wish the user
  # good luck.
  puts "Investment in #{stock} logged - good luck!"
end

#sellObject

Remove stocks from portfolio

FEATURE: Would be nice to be able to have multiple stocks from the same

company, bought at different times, and be able to pick which one
you're selling - rather than just automatically selling them all.


86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/portfolio/commands.rb', line 86

def sell
  if ARGV.length != 2
    return help()
  end
  
  stock_to_remove = ARGV[1].upcase
  portfolio = Store.instance.portfolio
  portfolio.reject! do |investment|
    investment['stock'].upcase == stock_to_remove
  end
  
  Store.instance.portfolio = portfolio
end

#showObject

Show watched stocks and user portfolio



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
# File 'lib/portfolio/commands.rb', line 4

def show
  # Get list of watched stocks:
  watched_stocks = Store.instance.watched_stocks
  
  # Get stock prices:
  watched_stocks.map! do |stock|
    {
      symbol: stock,
      price: StockPrices.instance.get_price_formatted(stock),
    }
  end
  
  # Get portfolio:
  portfolio = Formatter.instance.format_portfolio(Store.instance.portfolio)
  
  # Output watched stock table:
  Formatter.instance.output_table("Watched Stocks", [:symbol, :price], watched_stocks)
  
  # Output portfolio:
  Formatter.instance.output_table("Portfolio",
    [:symbol, :current_price, :number_owned, :price_paid, :commission, :profit],
    portfolio,
    ['Symbol', 'Current Price', 'Number Owned', 'Price Paid', 'Commission', 'Profit'],
  )
end

#watchObject

Start watching a stock



31
32
33
34
35
36
37
38
39
40
# File 'lib/portfolio/commands.rb', line 31

def watch
  if ARGV.length != 2
    return help()
  end
  
  watched_stocks = Store.instance.watched_stocks
  watched_stocks << ARGV[1].upcase
  
  Store.instance.watched_stocks = watched_stocks
end