/
enumeration.cr
49 lines (43 loc) · 1.24 KB
/
enumeration.cr
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
struct Money
class Currency
module Enumeration
include Enumerable(Currency)
# Lookup a currency with given *key* an returns a `Currency` instance on
# success, `nil` otherwise.
#
# ```
# Money::Currency.find("EUR") # => #<Money::Currency @id="eur">
# Money::Currency.find("FOO") # => nil
# ```
def find?(key : String | Symbol) : Currency?
table[key.to_s.downcase]?
end
# :ditto:
def []?(key : String | Symbol) : Currency?
find?(key)
end
# Lookup a currency with given *key* an returns a `Currency` instance on
# success, raises `UnknownCurrencyError` otherwise.
#
# ```
# Money::Currency.find("EUR") # => #<Money::Currency @id="eur">
# Money::Currency.find("FOO") # => raises UnknownCurrencyError
# ```
def find(key : String | Symbol) : Currency
find?(key) || raise UnknownCurrencyError.new("Can't find currency: #{key}")
end
# :ditto:
def [](key : String | Symbol) : Currency
find(key)
end
def all : Array(Currency)
table.values.sort!
end
def each(&) : Nil
all.each do |*args|
yield *args
end
end
end
end
end