Skip to content
This repository has been archived by the owner on Jul 2, 2018. It is now read-only.

Commit

Permalink
Merge branch 'release/1.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
danthorpe committed Nov 6, 2015
2 parents 3a3b7a3 + 13d4281 commit e137751
Show file tree
Hide file tree
Showing 12 changed files with 209 additions and 108 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 1.1.0
1. [[MNY-16](https://github.com/danthorpe/Money/pull/16)]: Grab bag of minor issues post 1.0 release.
* Cleans up some minor mistakes (spelling etc).
* Adds `NSCoding` conformance to `FXQuote` - so it can be persisted if needed.
* Adds `FXRemoteProviderType.quote(: BaseMoney, completion: Result<(BaseMoney, FXQuote, CounterMoney), FXError> -> Void) -> NSURLSessionDataTask` API. This is the nuts and bolts of the FX provider now. It returns as its result, the base money (i.e. the input), the quote (which includes the rate), and the counter money (i.e. the output). The `fx` method still exists, and it just unwraps the tuple to return the counter money. See the updated README.
2. [[MNY-17](https://github.com/danthorpe/Money/pull/17)]: There was an oversight in the functions in `DecimalNumberType` which accepts `NSDecimalNumberBehaviors` as an argument. These were unnecessary so I’ve removed them. Hence the minor version bump.

# 1.0.0
🎉🐝 Initial release of Money.
Expand Down
2 changes: 1 addition & 1 deletion Money.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "Money"
s.version = "1.0.0"
s.version = "1.1.0"
s.summary = "Swift types for working with Money."
s.description = <<-DESC
Expand Down
21 changes: 11 additions & 10 deletions Money/Decimal/Decimal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import ValueCoding
and scale rules for base 10 decimal arithmetic.
*/
public struct _Decimal<Behavior: DecimalNumberBehaviorType>: DecimalNumberType {

public typealias DecimalNumberBehavior = Behavior

/// Access the underlying decimal storage.
Expand Down Expand Up @@ -100,8 +101,8 @@ public struct _Decimal<Behavior: DecimalNumberBehaviorType>: DecimalNumberType {
- returns: another instance of this type.
*/
@warn_unused_result
public func subtract(other: _Decimal, withBehaviors behaviors: NSDecimalNumberBehaviors) -> _Decimal {
return _Decimal(storage: storage.subtract(other.storage, withBehaviors: behaviors))
public func subtract(other: _Decimal) -> _Decimal {
return _Decimal(storage: storage.subtract(other.storage, withBehaviors: DecimalNumberBehavior.decimalNumberBehaviors))
}

/**
Expand All @@ -112,8 +113,8 @@ public struct _Decimal<Behavior: DecimalNumberBehaviorType>: DecimalNumberType {
- returns: another instance of this type.
*/
@warn_unused_result
public func add(other: _Decimal, withBehaviors behaviors: NSDecimalNumberBehaviors) -> _Decimal {
return _Decimal(storage: storage.add(other.storage, withBehaviors: behaviors))
public func add(other: _Decimal) -> _Decimal {
return _Decimal(storage: storage.add(other.storage, withBehaviors: DecimalNumberBehavior.decimalNumberBehaviors))
}

/**
Expand All @@ -124,8 +125,8 @@ public struct _Decimal<Behavior: DecimalNumberBehaviorType>: DecimalNumberType {
- returns: another instance of this type.
*/
@warn_unused_result
public func multiplyBy(other: _Decimal, withBehaviors behaviors: NSDecimalNumberBehaviors) -> _Decimal {
return _Decimal(storage: storage.multiplyBy(other.storage, withBehaviors: behaviors))
public func multiplyBy(other: _Decimal) -> _Decimal {
return _Decimal(storage: storage.multiplyBy(other.storage, withBehaviors: DecimalNumberBehavior.decimalNumberBehaviors))
}

/**
Expand All @@ -136,8 +137,8 @@ public struct _Decimal<Behavior: DecimalNumberBehaviorType>: DecimalNumberType {
- returns: another instance of this type.
*/
@warn_unused_result
public func divideBy(other: _Decimal, withBehaviors behaviors: NSDecimalNumberBehaviors) -> _Decimal {
return _Decimal(storage: storage.divideBy(other.storage, withBehaviors: behaviors))
public func divideBy(other: _Decimal) -> _Decimal {
return _Decimal(storage: storage.divideBy(other.storage, withBehaviors: DecimalNumberBehavior.decimalNumberBehaviors))
}

/**
Expand All @@ -148,8 +149,8 @@ public struct _Decimal<Behavior: DecimalNumberBehaviorType>: DecimalNumberType {
- returns: another instance of this type.
*/
@warn_unused_result
public func remainder(other: _Decimal, withBehaviors behaviors: NSDecimalNumberBehaviors) -> _Decimal {
return _Decimal(storage: storage.remainder(other.storage, withBehaviors: behaviors))
public func remainder(other: _Decimal) -> _Decimal {
return _Decimal(storage: storage.remainder(other.storage, withBehaviors: DecimalNumberBehavior.decimalNumberBehaviors))
}
}

Expand Down
113 changes: 64 additions & 49 deletions Money/Decimal/DecimalNumberType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,53 @@

import Foundation

/**

# DecimalNumberBehaviorType

Defines the decimal number behavior, i.e. `NSDecimalNumberBehaviors`
of the type.

*/
public protocol DecimalNumberBehaviorType {

/// Specify the decimal number (i.e. rounding, scale etc) for base 10 calculations
static var decimalNumberBehaviors: NSDecimalNumberBehaviors { get }
}

public struct DecimalNumberBehavior {
/**

private static func behaviorWithRoundingMode(mode: NSRoundingMode) -> NSDecimalNumberBehaviors {
return NSDecimalNumberHandler(roundingMode: mode, scale: 38, raiseOnExactness: false, raiseOnOverflow: true, raiseOnUnderflow: true, raiseOnDivideByZero: true)
}
# DecimalNumberBehavior

This is a name space of types which conform to `DecimalNumberBehaviorType`
with common rounding modes. All have maximum precision, of 38 significant
digits.
*/
public struct DecimalNumberBehavior {

/// Plain rounding mode
public struct Plain: DecimalNumberBehaviorType {
public static let decimalNumberBehaviors = DecimalNumberBehavior.behaviorWithRoundingMode(.RoundPlain)
}

/// Round down mode
public struct RoundDown: DecimalNumberBehaviorType {
public static let decimalNumberBehaviors = DecimalNumberBehavior.behaviorWithRoundingMode(.RoundDown)
}

/// Round up mode
public struct RoundUp: DecimalNumberBehaviorType {
public static let decimalNumberBehaviors = DecimalNumberBehavior.behaviorWithRoundingMode(.RoundUp)
}

/// Bankers rounding mode, see `NSRoundingMode.RoundBankers` for info.
public struct Bankers: DecimalNumberBehaviorType {
public static let decimalNumberBehaviors = DecimalNumberBehavior.behaviorWithRoundingMode(.RoundBankers)
}

private static func behaviorWithRoundingMode(mode: NSRoundingMode, scale: Int16 = 38) -> NSDecimalNumberBehaviors {
return NSDecimalNumberHandler(roundingMode: mode, scale: 38, raiseOnExactness: false, raiseOnOverflow: true, raiseOnUnderflow: true, raiseOnDivideByZero: true)
}
}


Expand Down Expand Up @@ -93,82 +113,98 @@ public protocol DecimalNumberType: SignedNumberType, IntegerLiteralConvertible,
Subtract a matching `DecimalNumberType` from the receiver.

- parameter other: another instance of this type.
- parameter behaviors: an optional NSDecimalNumberBehaviors?
- returns: another instance of this type.
*/
@warn_unused_result
func subtract(_: Self, withBehaviors: NSDecimalNumberBehaviors) -> Self
func subtract(_: Self) -> Self

/**
Add a matching `DecimalNumberType` to the receiver.

- parameter other: another instance of this type.
- parameter behaviors: an optional NSDecimalNumberBehaviors?
- returns: another instance of this type.
*/
@warn_unused_result
func add(_: Self, withBehaviors: NSDecimalNumberBehaviors) -> Self
func add(_: Self) -> Self

/**
Multiply a matching `DecimalNumberType` with the receiver.

- parameter other: another instance of this type.
- parameter behaviors: an optional NSDecimalNumberBehaviors?
- returns: another instance of this type.
*/
@warn_unused_result
func multiplyBy(_: Self, withBehaviors: NSDecimalNumberBehaviors) -> Self
func multiplyBy(_: Self) -> Self

/**
Multiply another `DecimalNumberType` with the receiver. The other
`DecimalNumberType` must have the same underlying `DecimalStorageType` as
this `DecimalNumberType`.

- parameter other: another `DecimalNumberType` value of different type.
- parameter behaviors: an optional NSDecimalNumberBehaviors?
- returns: a different `DecimalNumberType` value.
*/
@warn_unused_result
func multiplyBy<Other: DecimalNumberType where Other.DecimalStorageType == DecimalStorageType>(_: Other, withBehaviors: NSDecimalNumberBehaviors) -> Other
func multiplyBy<Other: DecimalNumberType where Other.DecimalStorageType == DecimalStorageType>(_: Other) -> Other

/**
Divide the receiver by a matching `DecimalNumberType`.

- parameter other: another instance of this type.
- parameter behaviors: an optional NSDecimalNumberBehaviors?
- returns: another instance of this type.
*/
@warn_unused_result
func divideBy(_: Self, withBehaviors: NSDecimalNumberBehaviors) -> Self
func divideBy(_: Self) -> Self

/**
Divide the receiver by another `DecimalNumberType`. The other
`DecimalNumberType` must have the same underlying `DecimalStorageType` as
this `DecimalNumberType`.

- parameter other: another `DecimalNumberType` value of different type.
- parameter behaviors: an optional NSDecimalNumberBehaviors?
- returns: another instance of this type.
*/
@warn_unused_result
func divideBy<Other: DecimalNumberType where Other.DecimalStorageType == DecimalStorageType>(_: Other, withBehaviors: NSDecimalNumberBehaviors) -> Other
func divideBy<Other: DecimalNumberType where Other.DecimalStorageType == DecimalStorageType>(_: Other) -> Other

/**
The remainder of dividing another `DecimalNumberType` into the receiver.

- parameter other: another instance of this type.
- parameter behaviors: an optional NSDecimalNumberBehaviors?
- returns: another instance of this type.
*/
@warn_unused_result
func remainder(_: Self, withBehaviors: NSDecimalNumberBehaviors) -> Self
func remainder(_: Self) -> Self
}

public extension DecimalNumberType where DecimalStorageType == NSDecimalNumber {

@warn_unused_result
func multiplyBy<Other: DecimalNumberType where Other.DecimalStorageType == NSDecimalNumber>(other: Other) -> Other {
return Other(storage: storage.multiplyBy(other.storage, withBehaviors: Other.DecimalNumberBehavior.decimalNumberBehaviors) )
}

@warn_unused_result
func divideBy<Other: DecimalNumberType where Other.DecimalStorageType == NSDecimalNumber>(other: Other) -> Other {
return Other(storage: storage.divideBy(other.storage, withBehaviors: Other.DecimalNumberBehavior.decimalNumberBehaviors))
}
}

extension DecimalNumberType where Self.IntegerLiteralType == Int {

/// Get the reciprocal of the receiver.
public var reciprocal: Self {
return Self(integerLiteral: 1).divideBy(self)
}
}

// MARK: - Operators

// MARK: - Subtraction

@warn_unused_result
public func -<T: DecimalNumberType>(lhs: T, rhs: T) -> T {
return lhs.subtract(rhs, withBehaviors: T.DecimalNumberBehavior.decimalNumberBehaviors)
return lhs.subtract(rhs)
}

@warn_unused_result
Expand All @@ -191,18 +227,11 @@ public func -<T: DecimalNumberType>(lhs: T.FloatLiteralType, rhs: T) -> T {
return T(floatLiteral: lhs) - rhs
}

// MARK: - Remainder

@warn_unused_result
public func %<T: DecimalNumberType>(lhs: T, rhs: T) -> T {
return lhs.remainder(rhs, withBehaviors: T.DecimalNumberBehavior.decimalNumberBehaviors)
}

// MARK: - Addition

@warn_unused_result
public func +<T: DecimalNumberType>(lhs: T, rhs: T) -> T {
return lhs.add(rhs, withBehaviors: T.DecimalNumberBehavior.decimalNumberBehaviors)
return lhs.add(rhs)
}

@warn_unused_result
Expand All @@ -229,7 +258,7 @@ public func +<T: DecimalNumberType>(lhs: T.FloatLiteralType, rhs: T) -> T {

@warn_unused_result
public func *<T: DecimalNumberType>(lhs: T, rhs: T) -> T {
return lhs.multiplyBy(rhs, withBehaviors: T.DecimalNumberBehavior.decimalNumberBehaviors)
return lhs.multiplyBy(rhs)
}

@warn_unused_result
Expand Down Expand Up @@ -257,14 +286,14 @@ public func *<T, V where
T: DecimalNumberType,
V: DecimalNumberType,
T.DecimalStorageType == V.DecimalStorageType>(lhs: T, rhs: V) -> V {
return lhs.multiplyBy(rhs, withBehaviors: V.DecimalNumberBehavior.decimalNumberBehaviors)
return lhs.multiplyBy(rhs)
}

// MARK: - Division

@warn_unused_result
public func /<T: DecimalNumberType>(lhs: T, rhs: T) -> T {
return lhs.divideBy(rhs, withBehaviors: T.DecimalNumberBehavior.decimalNumberBehaviors)
return lhs.divideBy(rhs)
}

@warn_unused_result
Expand All @@ -282,28 +311,14 @@ public func /<T, V where
T: DecimalNumberType,
V: DecimalNumberType,
T.DecimalStorageType == V.DecimalStorageType>(lhs: T, rhs: V) -> V {
return lhs.divideBy(rhs, withBehaviors: V.DecimalNumberBehavior.decimalNumberBehaviors)
}

extension DecimalNumberType where DecimalStorageType == NSDecimalNumber {

@warn_unused_result
public func multiplyBy<Other: DecimalNumberType where Other.DecimalStorageType == NSDecimalNumber>(other: Other, withBehaviors behaviors: NSDecimalNumberBehaviors) -> Other {
return Other(storage: storage.multiplyBy(other.storage, withBehaviors: behaviors))
}

@warn_unused_result
public func divideBy<Other: DecimalNumberType where Other.DecimalStorageType == NSDecimalNumber>(other: Other, withBehaviors behaviors: NSDecimalNumberBehaviors) -> Other {
return Other(storage: storage.divideBy(other.storage, withBehaviors: behaviors))
}
return lhs.divideBy(rhs)
}

extension DecimalNumberType where Self.IntegerLiteralType == Int {
// MARK: - Remainder

/// Get the reciprocal of the receiver.
public var reciprocal: Self {
return Self(integerLiteral: 1).divideBy(self, withBehaviors: DecimalNumberBehavior.decimalNumberBehaviors)
}
@warn_unused_result
public func %<T: DecimalNumberType>(lhs: T, rhs: T) -> T {
return lhs.remainder(rhs)
}


Expand Down
Loading

0 comments on commit e137751

Please sign in to comment.