是否可以将Ruby货币列表过滤为仅有效货币?

时间:2017-04-06 16:13:56

标签: ruby currency money-rails

我在Rails应用中使用money gem进行货币转换,需要为表单选择标记生成货币列表。

建议的文件依赖于致电Money::Currency.table获取所有货币的清单,但这包括非等值货币(例如比特币)和旧货币(例如三种无效的津巴布韦元货币)。

有没有办法只获得有效的ISO货币代码列表而不维护我自己的清单?

1 个答案:

答案 0 :(得分:0)

金钱宝石确实包括separate lists,但不幸的是,它们是merged together

def load_currencies
  currencies = parse_currency_file("currency_iso.json")
  currencies.merge! parse_currency_file("currency_non_iso.json")
  currencies.merge! parse_currency_file("currency_backwards_compatible.json")
end

您可以手动调用该代码以仅获取currency_iso.json列表:

Money::Currency.send(:parse_currency_file, 'currency_iso.json')
#=> {
#   :aed=>{:priority=>100, :iso_code=>"AED", :name=>"United Arab Emirates Dirham", :symbol=>"د.إ", :alternate_symbols=>["DH", "Dhs"], :subunit=>"Fils", :subunit_to_unit=>100, :symbol_first=>true, :html_entity=>"", :decimal_mark=>".", :thousands_separator=>",", :iso_numeric=>"784", :smallest_denomination=>25},
#   :afn=>{:priority=>100, :iso_code=>"AFN", :name=>"Afghan Afghani", :symbol=>"؋", :alternate_symbols=>["Af", "Afs"], :subunit=>"Pul", :subunit_to_unit=>100, :symbol_first=>false, :html_entity=>"", :decimal_mark=>".", :thousands_separator=>",", :iso_numeric=>"971", :smallest_denomination=>100},
#   :all=>{:priority=>100, :iso_code=>"ALL", :name=>"Albanian Lek", :symbol=>"L", :disambiguate_symbol=>"Lek", :alternate_symbols=>["Lek"], :subunit=>"Qintar", :subunit_to_unit=>100, :symbol_first=>false, :html_entity=>"", :decimal_mark=>".", :thousands_separator=>",", :iso_numeric=>"008", :smallest_denomination=>100},
#   ...
#   :zar=>{:priority=>100, :iso_code=>"ZAR", :name=>"South African Rand", :symbol=>"R", :alternate_symbols=>[], :subunit=>"Cent", :subunit_to_unit=>100, :symbol_first=>true, :html_entity=>"R", :decimal_mark=>".", :thousands_separator=>",", :iso_numeric=>"710", :smallest_denomination=>10},
#   :zmk=>{:priority=>100, :iso_code=>"ZMK", :name=>"Zambian Kwacha", :symbol=>"ZK", :disambiguate_symbol=>"ZMK", :alternate_symbols=>[], :subunit=>"Ngwee", :subunit_to_unit=>100, :symbol_first=>false, :html_entity=>"", :decimal_mark=>".", :thousands_separator=>",", :iso_numeric=>"894", :smallest_denomination=>5},
#   :zmw=>{:priority=>100, :iso_code=>"ZMW", :name=>"Zambian Kwacha", :symbol=>"ZK", :disambiguate_symbol=>"ZMW", :alternate_symbols=>[], :subunit=>"Ngwee", :subunit_to_unit=>100, :symbol_first=>false, :html_entity=>"", :decimal_mark=>".", :thousands_separator=>",", :iso_numeric=>"967", :smallest_denomination=>5}}
# }

但请注意,这是一种私有方法,因此在将来的版本中可能会更改。