Class: Trader::Price

Inherits:
Object
  • Object
show all
Defined in:
lib/trade-o-matic/structs/price.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_currency, _amount) ⇒ Price

Returns a new instance of Price.



5
6
7
8
# File 'lib/trade-o-matic/structs/price.rb', line 5

def initialize(_currency, _amount)
  @currency = Currency.for_code _currency
  @amount = Standard.amount _amount
end

Instance Attribute Details

#amountObject (readonly)

Returns the value of attribute amount.



3
4
5
# File 'lib/trade-o-matic/structs/price.rb', line 3

def amount
  @amount
end

#currencyObject (readonly)

Returns the value of attribute currency.



3
4
5
# File 'lib/trade-o-matic/structs/price.rb', line 3

def currency
  @currency
end

Instance Method Details

#convert_to(_other_currency) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/trade-o-matic/structs/price.rb', line 14

def convert_to(_other_currency)
  _other_currency = Currency.for_code _other_currency

  return self if _other_currency == currency

  new_amount = @currency.convert amount, _other_currency
  Price.new _other_currency, new_amount
end

#pack(_amount) ⇒ Object



10
11
12
# File 'lib/trade-o-matic/structs/price.rb', line 10

def pack(_amount)
  currency.pack _amount
end

#to_sObject



23
24
25
# File 'lib/trade-o-matic/structs/price.rb', line 23

def to_s
  "#{amount} #{currency.code}"
end