Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Money/Sources/Money/Currency.swift.gyb
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
86 lines (77 sloc)
2.08 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% warning = "This file was automatically generated and should not be edited." | |
// ${warning} | |
/// A monetary unit. | |
public protocol CurrencyType { | |
/// The three letter ISO 4217 currency code. | |
static var code: String { get } | |
/// The name of the currency. | |
static var name: String { get } | |
/** | |
The number of decimal places used to express | |
any minor units for the currency. | |
For example, the US Dollar (USD) | |
has a minor unit (cents) | |
equal to 1/100 of a dollar, | |
and therefore takes 2 decimal places. | |
The Japanese Yen (JPY) | |
doesn't have a minor unit, | |
and therefore takes 0 decimal places. | |
*/ | |
static var minorUnit: Int { get } | |
} | |
/// Returns the ISO 4217 currency associated with a given code. | |
/// | |
/// Currency codes are checked according to a strict, case-sensitive equality comparison. | |
/// | |
/// - Important: This method returns only currencies defined in the `Money` module. | |
/// For example, | |
/// if you define a new type that adopts `CurrencyType`, | |
/// calling this method with that currency type's `code` returns `nil`. | |
/// | |
/// - Parameter code: The ISO 4217 currency code | |
/// - Returns: A `CurrencyType` type, if one | |
public func iso4217Currency(for code: String) -> CurrencyType.Type? { | |
switch code { | |
%{ | |
import csv | |
}% | |
% with open('../../Resources/iso4217.csv') as file: | |
% for row in csv.DictReader(file): | |
%{ | |
code = row["Code"] | |
}% | |
% if code: | |
case "${code}": return ${code}.self | |
%end | |
%end | |
%end | |
default: | |
return nil | |
} | |
} | |
%{ | |
import csv | |
}% | |
% with open('../../Resources/iso4217.csv') as file: | |
% for row in csv.DictReader(file): | |
%{ | |
code = row["Code"] | |
name = row["Name"] | |
minorUnit = row["MinorUnit"] | |
}% | |
% if code and name and minorUnit: | |
/// ${name} (${code}) | |
public enum ${code}: CurrencyType { | |
public static var code: String { | |
return "${code}" | |
} | |
public static var name: String { | |
return "${name}" | |
} | |
public static var minorUnit: Int { | |
return ${minorUnit} | |
} | |
} | |
%end | |
%end | |
%end |