1

My current config format seems so redundant. How could I convert it into the next expected config format?

My expected config is:

MoneyRails.configure do |config|

    register_currency("TWD", 100)
    register_currency("USD", 100)
    ....
end

My current config is:

MoneyRails.configure do |config|

  config.register_currency = {
    :priority            => 1,
    :iso_code            => "TWD",
    :name                => "TWD",
    :symbol              => "NT$ ",
    :symbol_first        => true,
    :subunit             => "Subcent",
    :subunit_to_unit     => 100,
    :thousands_separator => ",",
    :decimal_mark        => "."
  }

  config.register_currency = {
    :priority            => 1,
    :iso_code            => "USD",
    :name                => "USD",
    :symbol              => "$ ",
    :symbol_first        => true,
    :subunit             => "Subcent",
    :subunit_to_unit     => 100,
    :thousands_separator => ",",
    :decimal_mark        => "."
  }

  config.register_currency = {
    :priority            => 1,
    :iso_code            => "SGD",
    :name                => "SGD",
    :symbol              => "$ ",
    :symbol_first        => true,
    :subunit             => "Subcent",
    :subunit_to_unit     => 100,
    :thousands_separator => ",",
    :decimal_mark        => "."
  }

  config.register_currency = {
    :priority            => 1,
    :iso_code            => "THB",
    :name                => "THB",
    :symbol              => "$ ",
    :symbol_first        => true,
    :subunit             => "Subcent",
    :subunit_to_unit     => 100,
    :thousands_separator => ",",
    :decimal_mark        => "."
  }

  config.register_currency = {
    :priority            => 1,
    :iso_code            => "AUD",
    :name                => "AUD",
    :symbol              => "$ ",
    :symbol_first        => true,
    :subunit             => "Subcent",
    :subunit_to_unit     => 100,
    :thousands_separator => ",",
    :decimal_mark        => "."
  }

  config.register_currency = {
    :priority            => 1,
    :iso_code            => "KRW",
    :name                => "KRW",
    :symbol              => "$ ",
    :symbol_first        => true,
    :subunit             => "Subcent",
    :subunit_to_unit     => 100,
    :thousands_separator => ",",
    :decimal_mark        => "."
  }

  ...

end
4

3 回答 3

1

创建这个小模块怎么样?您只需要在配置对象中添加一个额外的属性:

module MoneyRails
  module MyApp
    def register_currency(config, name, subunit_to_unit)
      config.register_currency(options_hash(name, subunit_to_unit))
    end

    private

    def options_hash(name, subunit_to_unit)
      {
        :priority            => 1,
        :iso_code            => name,
        :name                => name,
        :symbol              => "$ ",
        :symbol_first        => true,
        :subunit             => "Subcent",
        :subunit_to_unit     => subunit_to_unit,
        :thousands_separator => ",",
        :decimal_mark        => "."
      }
    end
  end
end

请注意,我对其进行命名空间是为了不污染基本命名空间。把它改成你的休闲。

接着:

include MoneyRails::MyApp

MoneyRails.configure do |config|

    register_currency(config, "TWD", 100)
    register_currency(config, "USD", 100)
    ....
end
于 2015-11-01T14:07:25.623 回答
1
default_options = {
    :priority            => 1,
    :iso_code            => 'USD',
    :name                => 'USD',
    :symbol              => "$ ",
    :symbol_first        => true,
    :subunit             => "Subcent",
    :subunit_to_unit     => 100,
    :thousands_separator => ",",
    :decimal_mark        => "."
}

MoneyRails.configure do |config|
  config.register_currency = default_options
  config.register_currency = default_options.merge(name: 'TWD', iso_code: 'TWD', symbol: 'NT$')
  config.register_currency = default_options.merge(name: 'THB', iso_code: 'THB')
  config.register_currency = default_options.merge(name: 'SGD')
end

merge方法合并两个 Hash 实例并返回一个 Hash 对象。如果第二个 Hash 对象有一些与第一个匹配的键,那么第二个 Hashkey => value将替换/覆盖前一个。

于 2015-11-01T14:49:24.187 回答
0

例外情况相对较少,所以我倾向于将它们放在一个文件中,每种货币一行,以及货币名称缩写。例如,一行可能是:

TWD SYM:"NT$ "

(或者TWD SYM:NT$如果 的所有值:symbol最后都有一个空格)。

大多数行只有货币缩写,例如:

USD

然后,您将读取该文件并将其解析为一个数组,如下所示:

specific_bits = [
  ["TWD", { symbol: "NT$ " }],
  ["USD", {}],
  ["SGD", {} ],
  ["THB", { subunit_to_unit: 999 }],
  ["AUD", {}],
  ["KRW", {}],
  ["XYZ", { symbol: "XT$ ", subunit_to_unit: -5 }],
]

然后定义:

BASE_HASH = {
  priority:            1,
  symbol:              "$ ",
  symbol_first:        true,
  subunit:             "Subcent",
  subunit_to_unit:     100,
  thousands_separator: ",",
  decimal_mark:        "."
}

这使您可以非常简单地构造散列:

specific_bits.map do |name, exceptions|
  h = { :iso_code => name, :name => name }
  exceptions.each { |k,v| h[k] = v }
  BASE_HASH.merge h
end
  #=> [{:priority=>1, :symbol=>"NT$ ", :symbol_first=>true,    :subunit=>"Subcent",
  #    :subunit_to_unit=>100, :thousands_separator=>",", :decimal_mark=>".",
  #    :iso_code=>"TWD", :name=>"TWD"},
  #    {...
  #    ...]

如果您希望按键按特定顺序排列,这是一个小调整。

于 2015-11-01T15:21:51.647 回答