Class: Baron::Market

Inherits:
Object
  • Object
show all
Defined in:
lib/baron/market.rb

Overview

The market manages the open market pricing for shares in a company

The market price is affected by dividend payouts, share sales, and more. The price will generally increase for paying dividends, decrease for not paying a dividend, and decrease for sales of shares in the company.

Defined Under Namespace

Classes: InvalidStartingPrice

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rules) ⇒ Market

Initialize the market

Examples:

Baron::Market.new rules

Parameters:


25
26
27
28
# File 'lib/baron/market.rb', line 25

def initialize(rules)
  @rules = rules
  @current_prices = {}
end

Instance Attribute Details

#rulesBaron::Rules (readonly)

The rules of this game

Examples:

market.rules

Returns:


16
17
18
# File 'lib/baron/market.rb', line 16

def rules
  @rules
end

Instance Method Details

#add_company(company, starting_price) ⇒ void

This method returns an undefined value.

Add a company to the market

This action will generally be taken when the company's directors certificate is purchased.

Examples:

market.add_company(company, Baron::Money.new(90))

Parameters:


42
43
44
45
46
47
# File 'lib/baron/market.rb', line 42

def add_company(company, starting_price)
  amount = starting_price.amount
  fail InvalidStartingPrice unless
    rules.market_values.include?(amount)
  @current_prices[company] = amount
end

#change_price(company, steps) ⇒ void

This method returns an undefined value.

Change the price of a company by a specified number of steps

The price will be increased by the number of steps specified, or reduce it by the number of steps specified.

Examples:

market.reduce_price(comapny, 2) # increase the price by 2 steps

Parameters:


89
90
91
92
93
# File 'lib/baron/market.rb', line 89

def change_price(company, steps)
  market_values = rules.market_values
  index = market_values.find_index(price(company).amount)
  @current_prices[company] = market_values.fetch(index + steps)
end

#operating_orderArray<Baron::Company>

Gets the operating order of the companies

The operating order is determined by price, with the tiebreaker being the company that entered that space on the market first.

Examples:

market.operating_order

Returns:


71
72
73
74
75
# File 'lib/baron/market.rb', line 71

def operating_order
  @current_prices.keys.sort_by do |company|
    -@current_prices.fetch(company)
  end
end

#price(company) ⇒ Baron::Money

Get the current price for a share in a company

Examples:

market.price(company)

Parameters:

Returns:


57
58
59
# File 'lib/baron/market.rb', line 57

def price(company)
  Money.new @current_prices.fetch(company)
end