Skip to content

Commit

Permalink
Use macro to reduce duplicate code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Forgy authored and Eric Forgy committed Jul 4, 2020
1 parent 658e31f commit 791412b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 20 deletions.
9 changes: 3 additions & 6 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ name = "Currencies"
uuid = "0fd90b74-7c1f-579e-9252-02cd883047b9"
license = "MIT"
authors = ["Eric Forgy <[email protected]>", "ScottPJones <[email protected]>"]
version = "0.18.1"
version = "0.18.2"

[deps]
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
[compat]
julia = "1"

[extras]
JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"
Expand All @@ -14,6 +14,3 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
[targets]
build = ["JSON3"]
test = ["Test"]

[compat]
julia = "1"
3 changes: 0 additions & 3 deletions deps/build.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ const outputname = joinpath(@__DIR__, "currency-data.jl")
# First, check if currency-data.jl already exists
isfile(outputname) && exit()

# Make sure JSON3 is available
using Pkg
Pkg.add("JSON3")
using JSON3

const src = "https://pkgstore.datahub.io/core/country-codes/country-codes_json/data/471a2e653140ecdd7243cdcacfd66608/country-codes_json.json"
Expand Down
16 changes: 5 additions & 11 deletions src/Currencies.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,17 @@ currency(::Type{C}) where {C<:Currency} = C
"Returns the ISO 4217 minor unit associated with a currency"
function unit end

unit(S::Symbol) = _currency_data[S][2]

unit(::Type{Currency{S}}) where {S} = unit(S)

"Returns the ISO 4217 code associated with a currency"
function code end

code(S::Symbol) = _currency_data[S][3]

code(::Type{Currency{S}}) where {S} = code(S)

"Returns the ISO 4217 name associated with a currency"
function name end

name(S::Symbol) = _currency_data[S][4]

name(::Type{Currency{S}}) where {S} = name(S)
ms = [:unit, :code, :name]
for (i,m) in enumerate(ms)
@eval $m(S::Symbol) = _currency_data[S][$(i+1)]
@eval $m(::Type{Currency{S}}) where {S} = $m(S)
end

"Returns all currency symbols"
allsymbols() = keys(_currency_data)
Expand Down

0 comments on commit 791412b

Please sign in to comment.