Class: MoneyAttribute::Railtie
- Inherits:
-
Rails::Railtie
- Object
- Rails::Railtie
- MoneyAttribute::Railtie
- Defined in:
- lib/money_attribute/railtie.rb
Class Method Summary collapse
- .build_format(fmt) ⇒ Object
- .build_hash_format(fmt) ⇒ Object
- .build_locale_format ⇒ Object
- .register_custom_currencies! ⇒ Object
- .setup_locale_backend! ⇒ Object
- .translate_format(str) ⇒ Object
Class Method Details
.build_format(fmt) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/money_attribute/railtie.rb', line 32 def self.build_format(fmt) if %i[positive negative zero].any? { |k| fmt.key?(k) } build_hash_format(fmt) else translate_format(fmt[:format]) end end |
.build_hash_format(fmt) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/money_attribute/railtie.rb', line 40 def self.build_hash_format(fmt) { positive: translate_format(fmt[:positive] || fmt[:format]), negative: translate_format(fmt[:negative] || fmt[:format]), zero: translate_format(fmt[:zero] || fmt[:format]) } end |
.build_locale_format ⇒ Object
27 28 29 30 |
# File 'lib/money_attribute/railtie.rb', line 27 def self.build_locale_format fmt = I18n.t('number.currency.format', default: {}) { decimal: fmt[:separator], thousand: fmt[:delimiter], format: build_format(fmt) } end |
.register_custom_currencies! ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/money_attribute/railtie.rb', line 52 def self.register_custom_currencies! Array(MoneyAttribute.config.added_currencies).each do |currency_data| if currency_data.respond_to?(:values_at) code = currency_data[:currency] subunit = currency_data[:subunit] symbol = currency_data[:symbol] else code, subunit, symbol = *currency_data end ::Mint::Currency.register(code:, subunit:, symbol:) rescue KeyError => e unless e..include?('already registered') raise ArgumentError, "Invalid currency configuration: #{currency_data.inspect}. " \ "Each currency must have :currency, :subunit, and :symbol keys. Error: #{e.}" end end end |
.setup_locale_backend! ⇒ Object
23 24 25 |
# File 'lib/money_attribute/railtie.rb', line 23 def self.setup_locale_backend! ::Mint.locale_backend = method(:build_locale_format).to_proc end |
.translate_format(str) ⇒ Object
48 49 50 |
# File 'lib/money_attribute/railtie.rb', line 48 def self.translate_format(str) str.to_s.gsub('%n', '%<amount>f').gsub('%u', '%<symbol>s') end |