Skip to content

Commit

Permalink
Merge pull request #294 from typelevel/wip-new-currencies
Browse files Browse the repository at this point in the history
Add currencies ZAR and NAD.  Add MoneyContext.withAdditionalCurrencies
  • Loading branch information
garyKeorkunian authored Jan 23, 2018
2 parents e47b493 + fbd99cc commit c76920e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
4 changes: 4 additions & 0 deletions shared/src/main/scala/squants/market/Money.scala
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,8 @@ object XAU extends Currency("XAU", "Gold", "oz", 4)
object BTC extends Currency("BTC", "BitCoin", "B", 15)
object ETH extends Currency("ETH", "Ether", "\u039e", 15)
object LTC extends Currency("LTC", "Litecoin", "\u0141", 15)
object ZAR extends Currency("ZAR", "South African Rand", "R", 2)
object NAD extends Currency("NAD", "Namibian Dollar", "N$", 2)

/**
* Support for Money DSL
Expand Down Expand Up @@ -489,6 +491,8 @@ object MoneyConversions {
def ether = ETH
def LTC = Money(n, squants.market.LTC)
def liteCoin = LTC
def ZAR = Money(n, squants.market.ZAR)
def NAD = Money(n, squants.market.NAD)
}

class MoneyNumeric()(implicit mc: MoneyContext) extends Numeric[Money] {
Expand Down
15 changes: 11 additions & 4 deletions shared/src/main/scala/squants/market/MoneyContext.scala
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,16 @@ case class MoneyContext(
else 0

/**
* Create a copy of this context with a new list of rates
* @param rates List[CurrencyExchangeRate]
* @return
*/
* Create a copy of this context with additional currencies added to the existing set
* @param additionalCurrencies Set[Currency]
* @return
*/
def withAdditionalCurrencies(additionalCurrencies: Set[Currency]) = copy (currencies = currencies ++ additionalCurrencies)

/**
* Create a copy of this context with a new list of rates
* @param rates List[CurrencyExchangeRate]
* @return
*/
def withExchangeRates(rates: List[CurrencyExchangeRate]) = copy(rates = rates)
}
12 changes: 12 additions & 0 deletions shared/src/test/scala/squants/market/MoneyContextSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,16 @@ class MoneyContextSpec extends FlatSpec with Matchers {
newContext.currencies should be(moneyContext.currencies)
newContext.rates should be(newRates)
}

it should "return a copy with additional currencies" in {

object NMY extends Currency("NMY", "New Money", "$", 2)
object OTM extends Currency("OTM", "Other Money", "$", 2)

val additionalCurrencies = Set(NMY, OTM)
val newContext = moneyContext.withAdditionalCurrencies(additionalCurrencies)
newContext.defaultCurrency should be(moneyContext.defaultCurrency)
newContext.currencies should be(moneyContext.currencies ++ additionalCurrencies)
newContext.rates should be(moneyContext.rates)
}
}
2 changes: 2 additions & 0 deletions shared/src/test/scala/squants/market/MoneySpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,8 @@ class MoneySpec extends FlatSpec with Matchers {
d.ether should be(ETH(d))
d.LTC should be(Money(d, squants.market.LTC))
d.liteCoin should be(LTC(d))
d.ZAR should be(ZAR(d))
d.NAD should be(NAD(d))
}

it should "provide Numeric support within a MoneyContext with no Exchange Rates" in {
Expand Down

0 comments on commit c76920e

Please sign in to comment.