Class: Baron::Transaction

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

Overview

A transaction is the exchange of money, trains, certificates, or other purchasable items between two parties.

The parties involved in the transaction could be the players, companies, the bank pool, the initial offering, or other concepts in the game.

The transaction will have a buyer, and a seller. The buyer receives the buyer items and the seller receives the seller items.

The names buyer and seller are not important to the transaction, but merely a convenient metaphor as these actions usually involve cash.

Defined Under Namespace

Classes: InvalidItemsError, InvalidPartyError, NonTransferrableError

Constant Summary collapse

IS_CERTIFICATE =

A simple proc for determining if an item is a certificate

-> (item) { item.instance_of? Certificate }
IS_TRAIN =

A simple proc for determining if an item is a train

-> (item) { item.instance_of? Train }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buyer, buyer_items, seller, seller_items) ⇒ Transaction

Initialize the object

Examples:

transaction = Baron::Transaction.new(
  buyer, buyer_items, seller, seller_items
)

Parameters:

  • buyer (Object)
  • buyer_items (Array<Object>)
  • seller (Object)
  • seller_items (Array<Object>)

81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/baron/transaction.rb', line 81

def initialize(buyer, buyer_items, seller, seller_items)
  @buyer = buyer
  @buyer_items = buyer_items
  @seller = seller
  @seller_items = seller_items

  validate_items
  validate_transferrable
  validate_ownership
  notify_parties
  notify_item_ownership
end

Instance Attribute Details

#buyerObject (readonly)

One of the parties participating in the transacion, labelled as the buyer

Examples:

transaction = Baron::Transaction.new(
  buyer, buyer_items, seller, seller_items
)
transaction.buyer #=> buyer

Returns:

  • (Object)

31
32
33
# File 'lib/baron/transaction.rb', line 31

def buyer
  @buyer
end

#buyer_itemsArray<Object> (readonly)

The items the buyer is receiving

Examples:

transaction = Baron::Transaction.new(
  buyer, buyer_items, seller, seller_items
)
transaction.buyer_items #=> buyer_items

Returns:

  • (Array<Object>)

43
44
45
# File 'lib/baron/transaction.rb', line 43

def buyer_items
  @buyer_items
end

#sellerObject (readonly)

The second party participating in the transaction, labelled as the seller

Examples:

transaction = Baron::Transaction.new(
  buyer, buyer_items, seller, seller_items
)
transaction.seller #=> seller

Returns:

  • (Object)

55
56
57
# File 'lib/baron/transaction.rb', line 55

def seller
  @seller
end

#seller_itemsArray<Object> (readonly)

The items the seller is receiving

Examples:

transaction = Baron::Transaction.new(
  buyer, buyer_items, seller, seller_items
)
transaction.seller_items #=> seller_items

Returns:

  • (Array<Object>)

67
68
69
# File 'lib/baron/transaction.rb', line 67

def seller_items
  @seller_items
end

Instance Method Details

#credits(shareholder) ⇒ Array<Object>

The items which the shareholder received

Examples:

transaction = Baron::Transaction.new(
  buyer, buyer_items, seller, seller_items
)
transaction.credits(buyer) #=> buyuer_items

Parameters:

Returns:

  • (Array<Object>)

    The objects receieved


120
121
122
# File 'lib/baron/transaction.rb', line 120

def credits(shareholder)
  shareholder_effects(shareholder, :credit)
end

#debits(shareholder) ⇒ Array<Object>

The items which the shareholder transferred away

Examples:

transaction = Baron::Transaction.new(
  buyer, buyer_items, seller, seller_items
)
transaction.debits(buyer) #=> seller_items

Parameters:

Returns:

  • (Array<Object>)

    The objects transferred away


105
106
107
# File 'lib/baron/transaction.rb', line 105

def debits(shareholder)
  shareholder_effects(shareholder, :debit)
end

#incoming(shareholder, filter) ⇒ Array<Baron::Certificate>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The items the shareholder receieves in this transaction

Parameters:

  • shareholder (Baron::Shareholder)
  • filter (Proc)

    A selector such as IS_CERTIFICATE or IS_TRAIN

Returns:


130
131
132
# File 'lib/baron/transaction.rb', line 130

def incoming(shareholder, filter)
  credits(shareholder).select(&filter)
end

#outgoing(shareholder, filter) ⇒ Array<Baron::Certificate>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The items the shareholder loses in this transaction

Parameters:

  • shareholder (Baron::Shareholder)
  • filter (Proc)

    A selector such as IS_CERTIFICATE or IS_TRAIN

Returns:


140
141
142
# File 'lib/baron/transaction.rb', line 140

def outgoing(shareholder, filter)
  debits(shareholder).select(&filter)
end