/
Balance.swift
72 lines (57 loc) · 1.92 KB
/
Balance.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//
// Balance.swift
// SwiftBeanCountModel
//
// Created by Steffen Kötte on 2018-05-13.
// Copyright © 2018 Steffen Kötte. All rights reserved.
//
import Foundation
/// An assert that the balance of a given commodity is correct for the accout at the end of the given day
public struct Balance {
/// Date of the Balance
public let date: Date
/// `AccountName` of the Balance
public let accountName: AccountName
/// `Amount` of the Balance
public let amount: Amount
/// MetaData of the Balance
public let metaData: [String: String]
/// Create a Balance
///
/// - Parameters:
/// - date: date of the balance
/// - account: account
/// - amount: amount
public init(date: Date, accountName: AccountName, amount: Amount, metaData: [String: String] = [:]) {
self.date = date
self.accountName = accountName
self.amount = amount
self.metaData = metaData
}
}
extension Balance: CustomStringConvertible {
private static let dateFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
return dateFormatter
}()
/// Returns the price string for the ledger.
public var description: String {
var result = "\(Self.dateFormatter.string(from: date)) balance \(accountName) \(amount)"
if !metaData.isEmpty {
result += "\n\(metaData.map { " \($0): \"\($1)\"" }.joined(separator: "\n"))"
}
return result
}
}
extension Balance: Equatable {
/// Retuns if the two prices are equal
///
/// - Parameters:
/// - lhs: price 1
/// - rhs: price 2
/// - Returns: true if the prices are equal, false otherwise
public static func == (lhs: Balance, rhs: Balance) -> Bool {
lhs.date == rhs.date && lhs.accountName == rhs.accountName && lhs.amount == rhs.amount && lhs.metaData == rhs.metaData
}
}