maguire 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (253) hide show
  1. data/LICENSE +21 -0
  2. data/README.md +59 -0
  3. data/iso_data/seed_layer/aed.json +9 -0
  4. data/iso_data/seed_layer/afn.json +9 -0
  5. data/iso_data/seed_layer/all.json +9 -0
  6. data/iso_data/seed_layer/amd.json +9 -0
  7. data/iso_data/seed_layer/ang.json +10 -0
  8. data/iso_data/seed_layer/aoa.json +9 -0
  9. data/iso_data/seed_layer/ars.json +9 -0
  10. data/iso_data/seed_layer/aud.json +16 -0
  11. data/iso_data/seed_layer/awg.json +9 -0
  12. data/iso_data/seed_layer/azn.json +9 -0
  13. data/iso_data/seed_layer/bam.json +9 -0
  14. data/iso_data/seed_layer/bbd.json +9 -0
  15. data/iso_data/seed_layer/bdt.json +9 -0
  16. data/iso_data/seed_layer/bgn.json +9 -0
  17. data/iso_data/seed_layer/bhd.json +9 -0
  18. data/iso_data/seed_layer/bif.json +9 -0
  19. data/iso_data/seed_layer/bmd.json +9 -0
  20. data/iso_data/seed_layer/bnd.json +9 -0
  21. data/iso_data/seed_layer/bob.json +9 -0
  22. data/iso_data/seed_layer/bov.json +9 -0
  23. data/iso_data/seed_layer/brl.json +9 -0
  24. data/iso_data/seed_layer/bsd.json +9 -0
  25. data/iso_data/seed_layer/btn.json +9 -0
  26. data/iso_data/seed_layer/bwp.json +9 -0
  27. data/iso_data/seed_layer/byr.json +9 -0
  28. data/iso_data/seed_layer/bzd.json +9 -0
  29. data/iso_data/seed_layer/cad.json +9 -0
  30. data/iso_data/seed_layer/cdf.json +9 -0
  31. data/iso_data/seed_layer/che.json +9 -0
  32. data/iso_data/seed_layer/chf.json +10 -0
  33. data/iso_data/seed_layer/chw.json +9 -0
  34. data/iso_data/seed_layer/clf.json +9 -0
  35. data/iso_data/seed_layer/clp.json +9 -0
  36. data/iso_data/seed_layer/cny.json +9 -0
  37. data/iso_data/seed_layer/cop.json +9 -0
  38. data/iso_data/seed_layer/cou.json +9 -0
  39. data/iso_data/seed_layer/crc.json +9 -0
  40. data/iso_data/seed_layer/cuc.json +9 -0
  41. data/iso_data/seed_layer/cup.json +9 -0
  42. data/iso_data/seed_layer/cve.json +9 -0
  43. data/iso_data/seed_layer/czk.json +9 -0
  44. data/iso_data/seed_layer/djf.json +9 -0
  45. data/iso_data/seed_layer/dkk.json +11 -0
  46. data/iso_data/seed_layer/dop.json +9 -0
  47. data/iso_data/seed_layer/dzd.json +9 -0
  48. data/iso_data/seed_layer/egp.json +9 -0
  49. data/iso_data/seed_layer/ern.json +9 -0
  50. data/iso_data/seed_layer/etb.json +9 -0
  51. data/iso_data/seed_layer/eur.json +42 -0
  52. data/iso_data/seed_layer/fjd.json +9 -0
  53. data/iso_data/seed_layer/fkp.json +9 -0
  54. data/iso_data/seed_layer/gbp.json +12 -0
  55. data/iso_data/seed_layer/gel.json +9 -0
  56. data/iso_data/seed_layer/ghs.json +9 -0
  57. data/iso_data/seed_layer/gip.json +9 -0
  58. data/iso_data/seed_layer/gmd.json +9 -0
  59. data/iso_data/seed_layer/gnf.json +9 -0
  60. data/iso_data/seed_layer/gtq.json +9 -0
  61. data/iso_data/seed_layer/gyd.json +9 -0
  62. data/iso_data/seed_layer/hkd.json +9 -0
  63. data/iso_data/seed_layer/hnl.json +9 -0
  64. data/iso_data/seed_layer/hrk.json +9 -0
  65. data/iso_data/seed_layer/htg.json +9 -0
  66. data/iso_data/seed_layer/huf.json +9 -0
  67. data/iso_data/seed_layer/idr.json +9 -0
  68. data/iso_data/seed_layer/ils.json +9 -0
  69. data/iso_data/seed_layer/inr.json +10 -0
  70. data/iso_data/seed_layer/iqd.json +9 -0
  71. data/iso_data/seed_layer/irr.json +9 -0
  72. data/iso_data/seed_layer/isk.json +9 -0
  73. data/iso_data/seed_layer/jmd.json +9 -0
  74. data/iso_data/seed_layer/jod.json +9 -0
  75. data/iso_data/seed_layer/jpy.json +9 -0
  76. data/iso_data/seed_layer/kes.json +9 -0
  77. data/iso_data/seed_layer/kgs.json +9 -0
  78. data/iso_data/seed_layer/khr.json +9 -0
  79. data/iso_data/seed_layer/kmf.json +9 -0
  80. data/iso_data/seed_layer/kpw.json +9 -0
  81. data/iso_data/seed_layer/krw.json +9 -0
  82. data/iso_data/seed_layer/kwd.json +9 -0
  83. data/iso_data/seed_layer/kyd.json +9 -0
  84. data/iso_data/seed_layer/kzt.json +9 -0
  85. data/iso_data/seed_layer/lak.json +9 -0
  86. data/iso_data/seed_layer/lbp.json +9 -0
  87. data/iso_data/seed_layer/lkr.json +9 -0
  88. data/iso_data/seed_layer/lrd.json +9 -0
  89. data/iso_data/seed_layer/lsl.json +9 -0
  90. data/iso_data/seed_layer/ltl.json +9 -0
  91. data/iso_data/seed_layer/lyd.json +9 -0
  92. data/iso_data/seed_layer/mad.json +10 -0
  93. data/iso_data/seed_layer/mdl.json +9 -0
  94. data/iso_data/seed_layer/mga.json +9 -0
  95. data/iso_data/seed_layer/mkd.json +9 -0
  96. data/iso_data/seed_layer/mmk.json +9 -0
  97. data/iso_data/seed_layer/mnt.json +9 -0
  98. data/iso_data/seed_layer/mop.json +9 -0
  99. data/iso_data/seed_layer/mro.json +9 -0
  100. data/iso_data/seed_layer/mur.json +9 -0
  101. data/iso_data/seed_layer/mvr.json +9 -0
  102. data/iso_data/seed_layer/mwk.json +9 -0
  103. data/iso_data/seed_layer/mxn.json +9 -0
  104. data/iso_data/seed_layer/mxv.json +9 -0
  105. data/iso_data/seed_layer/myr.json +9 -0
  106. data/iso_data/seed_layer/mzn.json +9 -0
  107. data/iso_data/seed_layer/nad.json +9 -0
  108. data/iso_data/seed_layer/ngn.json +9 -0
  109. data/iso_data/seed_layer/nio.json +9 -0
  110. data/iso_data/seed_layer/nok.json +11 -0
  111. data/iso_data/seed_layer/npr.json +9 -0
  112. data/iso_data/seed_layer/nzd.json +13 -0
  113. data/iso_data/seed_layer/omr.json +9 -0
  114. data/iso_data/seed_layer/pab.json +9 -0
  115. data/iso_data/seed_layer/pen.json +9 -0
  116. data/iso_data/seed_layer/pgk.json +9 -0
  117. data/iso_data/seed_layer/php.json +9 -0
  118. data/iso_data/seed_layer/pkr.json +9 -0
  119. data/iso_data/seed_layer/pln.json +9 -0
  120. data/iso_data/seed_layer/pyg.json +9 -0
  121. data/iso_data/seed_layer/qar.json +9 -0
  122. data/iso_data/seed_layer/ron.json +9 -0
  123. data/iso_data/seed_layer/rsd.json +9 -0
  124. data/iso_data/seed_layer/rub.json +9 -0
  125. data/iso_data/seed_layer/rwf.json +9 -0
  126. data/iso_data/seed_layer/sar.json +9 -0
  127. data/iso_data/seed_layer/sbd.json +9 -0
  128. data/iso_data/seed_layer/scr.json +9 -0
  129. data/iso_data/seed_layer/sdg.json +9 -0
  130. data/iso_data/seed_layer/sek.json +9 -0
  131. data/iso_data/seed_layer/sgd.json +9 -0
  132. data/iso_data/seed_layer/shp.json +9 -0
  133. data/iso_data/seed_layer/sll.json +9 -0
  134. data/iso_data/seed_layer/sos.json +9 -0
  135. data/iso_data/seed_layer/srd.json +9 -0
  136. data/iso_data/seed_layer/ssp.json +9 -0
  137. data/iso_data/seed_layer/std.json +9 -0
  138. data/iso_data/seed_layer/svc.json +9 -0
  139. data/iso_data/seed_layer/syp.json +9 -0
  140. data/iso_data/seed_layer/szl.json +9 -0
  141. data/iso_data/seed_layer/thb.json +9 -0
  142. data/iso_data/seed_layer/tjs.json +9 -0
  143. data/iso_data/seed_layer/tmt.json +9 -0
  144. data/iso_data/seed_layer/tnd.json +9 -0
  145. data/iso_data/seed_layer/top.json +9 -0
  146. data/iso_data/seed_layer/try.json +9 -0
  147. data/iso_data/seed_layer/ttd.json +9 -0
  148. data/iso_data/seed_layer/twd.json +9 -0
  149. data/iso_data/seed_layer/tzs.json +9 -0
  150. data/iso_data/seed_layer/uah.json +9 -0
  151. data/iso_data/seed_layer/ugx.json +9 -0
  152. data/iso_data/seed_layer/usd.json +27 -0
  153. data/iso_data/seed_layer/usn.json +9 -0
  154. data/iso_data/seed_layer/uyi.json +9 -0
  155. data/iso_data/seed_layer/uyu.json +9 -0
  156. data/iso_data/seed_layer/uzs.json +9 -0
  157. data/iso_data/seed_layer/vef.json +9 -0
  158. data/iso_data/seed_layer/vnd.json +9 -0
  159. data/iso_data/seed_layer/vuv.json +9 -0
  160. data/iso_data/seed_layer/wst.json +9 -0
  161. data/iso_data/seed_layer/xaf.json +14 -0
  162. data/iso_data/seed_layer/xag.json +9 -0
  163. data/iso_data/seed_layer/xau.json +9 -0
  164. data/iso_data/seed_layer/xba.json +9 -0
  165. data/iso_data/seed_layer/xbb.json +9 -0
  166. data/iso_data/seed_layer/xbc.json +9 -0
  167. data/iso_data/seed_layer/xbd.json +9 -0
  168. data/iso_data/seed_layer/xcd.json +16 -0
  169. data/iso_data/seed_layer/xdr.json +9 -0
  170. data/iso_data/seed_layer/xof.json +16 -0
  171. data/iso_data/seed_layer/xpd.json +9 -0
  172. data/iso_data/seed_layer/xpf.json +11 -0
  173. data/iso_data/seed_layer/xpt.json +9 -0
  174. data/iso_data/seed_layer/xsu.json +9 -0
  175. data/iso_data/seed_layer/xts.json +9 -0
  176. data/iso_data/seed_layer/xua.json +9 -0
  177. data/iso_data/seed_layer/xxx.json +9 -0
  178. data/iso_data/seed_layer/yer.json +9 -0
  179. data/iso_data/seed_layer/zar.json +11 -0
  180. data/iso_data/seed_layer/zmw.json +9 -0
  181. data/iso_data/seed_layer/zwl.json +9 -0
  182. data/iso_data/symbol_layer/aud.json +3 -0
  183. data/iso_data/symbol_layer/aud.json~ +17 -0
  184. data/iso_data/symbol_layer/cad.json +3 -0
  185. data/iso_data/symbol_layer/cad.json~ +10 -0
  186. data/iso_data/symbol_layer/chf.json +3 -0
  187. data/iso_data/symbol_layer/chf.json~ +11 -0
  188. data/iso_data/symbol_layer/cny.json +4 -0
  189. data/iso_data/symbol_layer/cny.json~ +11 -0
  190. data/iso_data/symbol_layer/dkk.json +3 -0
  191. data/iso_data/symbol_layer/dkk.json~ +12 -0
  192. data/iso_data/symbol_layer/eur.json +4 -0
  193. data/iso_data/symbol_layer/eur.json~ +44 -0
  194. data/iso_data/symbol_layer/gbp.json +4 -0
  195. data/iso_data/symbol_layer/gbp.json~ +14 -0
  196. data/iso_data/symbol_layer/hkd.json +3 -0
  197. data/iso_data/symbol_layer/hkd.json~ +10 -0
  198. data/iso_data/symbol_layer/ils.json +4 -0
  199. data/iso_data/symbol_layer/ils.json~ +11 -0
  200. data/iso_data/symbol_layer/inr.json +4 -0
  201. data/iso_data/symbol_layer/inr.json~ +12 -0
  202. data/iso_data/symbol_layer/jpy.json +4 -0
  203. data/iso_data/symbol_layer/jpy.json~ +11 -0
  204. data/iso_data/symbol_layer/khr.json +4 -0
  205. data/iso_data/symbol_layer/khr.json~ +11 -0
  206. data/iso_data/symbol_layer/kpw.json +4 -0
  207. data/iso_data/symbol_layer/kpw.json~ +11 -0
  208. data/iso_data/symbol_layer/krw.json +4 -0
  209. data/iso_data/symbol_layer/krw.json~ +11 -0
  210. data/iso_data/symbol_layer/lkr.json +4 -0
  211. data/iso_data/symbol_layer/lkr.json~ +11 -0
  212. data/iso_data/symbol_layer/mur.json +4 -0
  213. data/iso_data/symbol_layer/mur.json~ +11 -0
  214. data/iso_data/symbol_layer/mxn.json +3 -0
  215. data/iso_data/symbol_layer/mxn.json~ +10 -0
  216. data/iso_data/symbol_layer/ngn.json +4 -0
  217. data/iso_data/symbol_layer/ngn.json~ +11 -0
  218. data/iso_data/symbol_layer/nok.json +3 -0
  219. data/iso_data/symbol_layer/nok.json~ +12 -0
  220. data/iso_data/symbol_layer/npr.json +4 -0
  221. data/iso_data/symbol_layer/npr.json~ +11 -0
  222. data/iso_data/symbol_layer/php.json +4 -0
  223. data/iso_data/symbol_layer/php.json~ +11 -0
  224. data/iso_data/symbol_layer/pkr.json +4 -0
  225. data/iso_data/symbol_layer/pkr.json~ +11 -0
  226. data/iso_data/symbol_layer/rub.json +4 -0
  227. data/iso_data/symbol_layer/rub.json~ +11 -0
  228. data/iso_data/symbol_layer/scr.json +3 -0
  229. data/iso_data/symbol_layer/scr.json~ +10 -0
  230. data/iso_data/symbol_layer/sek.json +3 -0
  231. data/iso_data/symbol_layer/sek.json~ +10 -0
  232. data/iso_data/symbol_layer/thb.json +4 -0
  233. data/iso_data/symbol_layer/thb.json~ +11 -0
  234. data/iso_data/symbol_layer/try.json +4 -0
  235. data/iso_data/symbol_layer/try.json~ +11 -0
  236. data/iso_data/symbol_layer/uah.json +4 -0
  237. data/iso_data/symbol_layer/uah.json~ +11 -0
  238. data/iso_data/symbol_layer/usd.json +3 -0
  239. data/iso_data/symbol_layer/usd.json~ +28 -0
  240. data/iso_data/symbol_layer/vnd.json +4 -0
  241. data/iso_data/symbol_layer/vnd.json~ +11 -0
  242. data/iso_data/symbol_layer/zar.json +3 -0
  243. data/iso_data/symbol_layer/zar.json~ +12 -0
  244. data/lib/maguire/currency.rb +29 -0
  245. data/lib/maguire/data_set.rb +61 -0
  246. data/lib/maguire/hash.rb +13 -0
  247. data/lib/maguire/locale.rb +165 -0
  248. data/lib/maguire/version.rb +3 -0
  249. data/lib/maguire.rb +47 -0
  250. data/locale/en_CA.json~ +26 -0
  251. data/locale/en_US.json +26 -0
  252. data/locale/en_US.json~ +50 -0
  253. metadata +360 -0
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "Baht",
3
+ "code": "THB",
4
+ "number": "764",
5
+ "minor_units": 2,
6
+ "symbol": "฿",
7
+ "symbol_html": "฿",
8
+ "countries": [
9
+ "THAILAND"
10
+ ]
11
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "symbol": "₺",
3
+ "symbol_html": "₺"
4
+ }
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "Turkish Lira",
3
+ "code": "TRY",
4
+ "number": "949",
5
+ "minor_units": 2,
6
+ "symbol": "₺",
7
+ "symbol_html": "₺",
8
+ "countries": [
9
+ "TURKEY"
10
+ ]
11
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "symbol": "₴",
3
+ "symbol_html": "₴"
4
+ }
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "Hryvnia",
3
+ "code": "UAH",
4
+ "number": "980",
5
+ "minor_units": 2,
6
+ "symbol": "₴",
7
+ "symbol_html": "₴",
8
+ "countries": [
9
+ "UKRAINE"
10
+ ]
11
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "symbol": "$"
3
+ }
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "US Dollar",
3
+ "code": "USD",
4
+ "number": "840",
5
+ "minor_units": 2,
6
+ "symbol": "$",
7
+ "countries": [
8
+ "AMERICAN SAMOA",
9
+ "BONAIRE, SINT EUSTATIUS AND SABA",
10
+ "BRITISH INDIAN OCEAN TERRITORY",
11
+ "ECUADOR",
12
+ "EL SALVADOR",
13
+ "GUAM",
14
+ "HAITI",
15
+ "MARSHALL ISLANDS",
16
+ "MICRONESIA, FEDERATED STATES OF",
17
+ "NORTHERN MARIANA ISLANDS",
18
+ "PALAU",
19
+ "PANAMA",
20
+ "PUERTO RICO",
21
+ "TIMOR-LESTE",
22
+ "TURKS AND CAICOS ISLANDS",
23
+ "UNITED STATES",
24
+ "UNITED STATES MINOR OUTLYING ISLANDS",
25
+ "VIRGIN ISLANDS (BRITISH)",
26
+ "VIRGIN ISLANDS (U.S.)"
27
+ ]
28
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "symbol": "₫",
3
+ "symbol_html": "₫"
4
+ }
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "Dong",
3
+ "code": "VND",
4
+ "number": "704",
5
+ "minor_units": 0,
6
+ "symbol": "₫",
7
+ "symbol_html": "₫",
8
+ "countries": [
9
+ "VIET NAM"
10
+ ]
11
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "symbol": "R"
3
+ }
@@ -0,0 +1,12 @@
1
+ {
2
+ "name": "Rand",
3
+ "code": "ZAR",
4
+ "number": "710",
5
+ "minor_units": 2,
6
+ "symbol": "R",
7
+ "countries": [
8
+ "LESOTHO",
9
+ "NAMIBIA",
10
+ "SOUTH AFRICA"
11
+ ]
12
+ }
@@ -0,0 +1,29 @@
1
+ require 'json'
2
+ require 'ostruct'
3
+
4
+ module Maguire
5
+ class Currency < OpenStruct
6
+ def initialize(iso_data={})
7
+ @data = iso_data
8
+ super
9
+ end
10
+
11
+ def precision
12
+ 10 ** minor_units
13
+ end
14
+
15
+ def inspect
16
+ "<##{self.class} name=#{name} code=#{code}>"
17
+ end
18
+
19
+ def overlay(locale_data={})
20
+ Currency.new(Maguire::Hash.merge([@data, locale_data]))
21
+ end
22
+
23
+ class << self
24
+ def coded(code)
25
+ self.new(Maguire.data_paths.load(code.downcase))
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,61 @@
1
+ require 'maguire/hash'
2
+
3
+ module Maguire
4
+ class DataSet
5
+ include Enumerable
6
+
7
+ class NoDataFound < StandardError; end
8
+
9
+ def initialize()
10
+ @cache = {}
11
+ @paths = []
12
+ end
13
+
14
+ def each(&block)
15
+ @paths.each(&block)
16
+ end
17
+
18
+ def <<(path)
19
+ clear_cache!
20
+ @paths << path
21
+ end
22
+
23
+ def clear_cache!
24
+ @cache = {}
25
+ end
26
+
27
+ def clear
28
+ clear_cache!
29
+ @paths = []
30
+ end
31
+
32
+ def load(id)
33
+ return @cache[id.to_sym] if @cache && @cache[id.to_sym]
34
+
35
+ data_sets = map do |data_path|
36
+ path = data_path.join("#{id}.json")
37
+ if File.exists?(path)
38
+ JSON.parse(File.read(path), symbolize_names: true)
39
+ else
40
+ {}
41
+ end
42
+ end
43
+
44
+ merged_data = merge_data(data_sets)
45
+ if merged_data.empty?
46
+ raise NoDataFound.new(@paths.join(':'))
47
+ end
48
+
49
+ @cache[id.to_sym] = merged_data
50
+ end
51
+
52
+ def merge_data(data_sets)
53
+ merged_set = Maguire::Hash.merge(data_sets)
54
+ if merged_set[:enabled] == false
55
+ nil
56
+ else
57
+ merged_set
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,13 @@
1
+ module Maguire
2
+ module Hash
3
+ def self.merge(hashes)
4
+ return hashes.first if hashes.size == 1
5
+
6
+ hashes.inject do |acc, hash|
7
+ acc.merge(hash) do |key, old_value, new_value|
8
+ new_value || old_value
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,165 @@
1
+ require 'json'
2
+
3
+ module Maguire
4
+ class Locale
5
+ class NotSupportedError < StandardError; end
6
+
7
+ attr_reader :locale, :code, :symbol, :locale
8
+
9
+ def initialize(locale, locale_data={})
10
+ @locale = locale
11
+
12
+ @positive_formatting = parse_layout(locale_data[:layouts][:positive])
13
+ @negative_formatting = parse_layout(locale_data[:layouts][:negative])
14
+
15
+ @currency_overlays = locale_data
16
+ end
17
+
18
+ def inspect
19
+ "<##{self.class} locale=#{locale}>"
20
+ end
21
+
22
+ def format(value, currency, options={})
23
+ major_value = value.abs / currency.precision
24
+ minor_value = round(value.abs - major_value * currency.precision)
25
+
26
+ overlay = @currency_overlays[currency.code.downcase.to_sym]
27
+ currency = currency.overlay(overlay) if overlay
28
+
29
+ groups = split_value_into_groups(major_value)
30
+
31
+ formatting = value >= 0 ?
32
+ @positive_formatting : @negative_formatting
33
+
34
+ symbol = currency.symbol
35
+ if options[:html] && currency.symbol_html
36
+ symbol = currency.symbol_html
37
+ end
38
+
39
+ strip_insignificant_zeros = options[:strip_insignificant_zeros]
40
+ if options[:no_minor_units] || currency.minor_units == 0
41
+ minor_value = 0
42
+ strip_insignificant_zeros = true
43
+ end
44
+
45
+ if strip_insignificant_zeros && minor_value == 0
46
+ minor_value = ""
47
+ decimal_symbol = ""
48
+ else
49
+ minor_value = minor_value.to_s.rjust(currency.minor_units, "0")
50
+ decimal_symbol = formatting[:decimal_symbol]
51
+ end
52
+
53
+ formatting[:layout] % {
54
+ symbol: symbol,
55
+ code: currency.code,
56
+ decimal: decimal_symbol,
57
+ major_value: groups.join(formatting[:digit_grouping_symbol]),
58
+ minor_value: minor_value
59
+ }
60
+ end
61
+
62
+ private
63
+
64
+ SOUTH_ASIAN_GROUPING_RE = /([0-9]{2}[^0-9]){3}[0-9]{3}[^0-9]/
65
+ GROUPS_OF_FOUR_RE = /([0-9]{4}[^0-9])+/
66
+ GROUPS_OF_THREE_RE = /([0-9]{3}[^0-9]){3}+/
67
+
68
+ def parse_groups_in_south_asian_style(layout)
69
+ @group_numbers_in_south_asian_style = true
70
+ digit_grouping_symbol = layout.match(/1([^0-9]*)23/)[1]
71
+ layout.gsub!(["1", "23", "45", "67", "890"].join(digit_grouping_symbol), "%{major_value}")
72
+ digit_grouping_symbol
73
+ end
74
+
75
+ def parse_groups_of_four(layout)
76
+ @group_numbers_in_fours = true
77
+ digit_grouping_symbol = layout.match(/12([^0-9]*)3456/)[1]
78
+ layout.gsub!(["12", "3456", "7890"].join(digit_grouping_symbol), "%{major_value}")
79
+ digit_grouping_symbol
80
+ end
81
+
82
+ def parse_groups_of_three(layout)
83
+ @group_numbers_in_threes = true
84
+ digit_grouping_symbol = layout.match(/1([^0-9]*)234/)[1]
85
+ layout.gsub!(["1", "234", "567", "890"].join(digit_grouping_symbol), "%{major_value}")
86
+ digit_grouping_symbol
87
+ end
88
+
89
+ # Currency layouts are defined using a standard format of:
90
+ # 1234567890.12 USD
91
+ # 1,23,45,67,890.12 USD
92
+ # 12,3456,7890.12 USD
93
+ def parse_layout(layout)
94
+ layout = layout.dup
95
+
96
+ digit_grouping_symbol =
97
+ if layout =~ SOUTH_ASIAN_GROUPING_RE
98
+ parse_groups_in_south_asian_style(layout)
99
+ elsif layout =~ GROUPS_OF_FOUR_RE
100
+ parse_groups_of_four(layout)
101
+ elsif layout =~ GROUPS_OF_THREE_RE
102
+ parse_groups_of_three(layout)
103
+ end
104
+
105
+ layout.gsub!("USD", "%{code}")
106
+ layout.gsub!("$", "%{symbol}")
107
+ layout.gsub!("12", "%{minor_value}")
108
+ decimal_symbol = layout.match(/major_value}(.*)%{minor_value/)[1]
109
+ layout.gsub!(decimal_symbol, "%{decimal}")
110
+
111
+ {
112
+ layout: layout,
113
+ decimal_symbol: decimal_symbol,
114
+ digit_grouping_symbol: digit_grouping_symbol
115
+ }
116
+ end
117
+
118
+ def round(value)
119
+ value.to_i
120
+ end
121
+
122
+ def break_off(value, number)
123
+ len = value.length
124
+ number = len if number > len
125
+ [value.slice(0, len - number), value.slice(len - number, len)]
126
+ end
127
+
128
+ def split_value_into_groups_of(value, number)
129
+ groups = []
130
+ while value && value.length > 0
131
+ (value, partial) = break_off(value, number)
132
+ groups.unshift(partial)
133
+ end
134
+ groups
135
+ end
136
+
137
+ def split_value_into_groups(value)
138
+ value = value.to_s
139
+
140
+ if @group_numbers_in_threes
141
+ split_value_into_groups_of(value, 3)
142
+ elsif @group_numbers_in_fours
143
+ split_value_into_groups_of(value, 4)
144
+ elsif @group_numbers_in_south_asian_style
145
+ (value, partial) = break_off(value, 3)
146
+
147
+ split_value_into_groups_of(value, 2) << partial
148
+ else
149
+ []
150
+ end
151
+ end
152
+
153
+ class << self
154
+ def lookup(options)
155
+ locale = "#{options[:lang].downcase}_#{options[:country].upcase}"
156
+ data = Maguire.locale_paths.load(locale)
157
+ if data.nil?
158
+ raise Locale::NotSupportedError.new("The locale #{locale} isn't supported")
159
+ else
160
+ self.new(locale, data)
161
+ end
162
+ end
163
+ end
164
+ end
165
+ end
@@ -0,0 +1,3 @@
1
+ module Maguire
2
+ VERSION = '0.1.0'
3
+ end
data/lib/maguire.rb ADDED
@@ -0,0 +1,47 @@
1
+ require 'json'
2
+ require 'pathname'
3
+
4
+ lib_path = File.expand_path('../../lib', __FILE__)
5
+ $LOAD_PATH.unshift(lib_path)
6
+
7
+ require 'maguire/currency'
8
+ require 'maguire/data_set'
9
+ require 'maguire/locale'
10
+ require 'maguire/version'
11
+
12
+ module Maguire
13
+ class << self
14
+
15
+ attr_accessor :root_path, :data_paths, :locale_paths, :default_locale
16
+
17
+ def reset_data_paths
18
+ data_paths.clear
19
+ data_paths << root_path + 'iso_data/seed_layer' << root_path + 'iso_data/symbol_layer'
20
+ end
21
+
22
+ def reset_locale_paths
23
+ locale_paths.clear
24
+ locale_paths << root_path + 'locale'
25
+ end
26
+
27
+ def format(money, options={})
28
+ currency = Currency.coded(money[:currency].downcase)
29
+ locale = Locale.lookup(options[:locale] || Maguire.default_locale)
30
+
31
+ locale.format(money[:value], currency, options)
32
+ end
33
+ end
34
+
35
+ self.root_path = Pathname.new(__FILE__) + '../..'
36
+
37
+ self.data_paths = Maguire::DataSet.new
38
+ self.reset_data_paths
39
+
40
+ self.locale_paths = Maguire::DataSet.new
41
+ self.reset_locale_paths
42
+
43
+ self.default_locale = {
44
+ lang: "en",
45
+ country: "US"
46
+ }
47
+ end
@@ -0,0 +1,26 @@
1
+ {
2
+ "formats": {
3
+ "positive": "$1,234,567,890.12",
4
+ "negative": "($1,234,567,890.12)"
5
+ },
6
+
7
+ "aud": {
8
+ "symbol": "$A"
9
+ },
10
+
11
+ "brl": {
12
+ "symbol": "$R"
13
+ },
14
+
15
+ "hkd": {
16
+ "symbol": "$HK"
17
+ },
18
+
19
+ "mxn": {
20
+ "symbol": "$Mex"
21
+ },
22
+
23
+ "usd": {
24
+ "symbol": "$US"
25
+ }
26
+ }
data/locale/en_US.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "layouts": {
3
+ "positive": "$1,234,567,890.12",
4
+ "negative": "-$1,234,567,890.12"
5
+ },
6
+
7
+ "aud": {
8
+ "symbol": "A$"
9
+ },
10
+
11
+ "brl": {
12
+ "symbol": "R$"
13
+ },
14
+
15
+ "cad": {
16
+ "symbol": "C$"
17
+ },
18
+
19
+ "hkd": {
20
+ "symbol": "HK$"
21
+ },
22
+
23
+ "mxn": {
24
+ "symbol": "Mex$"
25
+ }
26
+ }
@@ -0,0 +1,50 @@
1
+ {
2
+ "formats": {
3
+ "positive": "$1,234,567,890.12",
4
+ "negative": "($1,234,567,890.12)"
5
+ },
6
+
7
+ "aud": {
8
+ "symbol": "A$"
9
+ },
10
+
11
+ "brl": {
12
+ "symbol": "R$"
13
+ },
14
+
15
+ "cad": {
16
+ "symbol": "C$"
17
+ },
18
+
19
+ "chf": {
20
+ "symbol": "Fr."
21
+ },
22
+
23
+ "dkk": {
24
+ "symbol": "kr."
25
+ },
26
+
27
+ "hkd": {
28
+ "symbol": "HK$"
29
+ },
30
+
31
+ "mxn": {
32
+ "symbol": "Mex$"
33
+ },
34
+
35
+ "nok": {
36
+ "symbol": "kr."
37
+ },
38
+
39
+ "scr": {
40
+ "symbol": "SR"
41
+ },
42
+
43
+ "sek": {
44
+ "symbol": "kr"
45
+ },
46
+
47
+ "zar": {
48
+ "symbol": "R"
49
+ }
50
+ }