Sha256: 0fd223d6f00e48675489e93dbb8adee422545c7359f81a38535299eee1e206ac

Contents?: true

Size: 1.29 KB

Versions: 29

Compression:

Stored size: 1.29 KB

Contents

module AccountingjsSerializer

  def self.serialize(money)
    {
      'value' => money.to_f,
      'options' => {
        'symbol' => money.symbol,
        'decimal' => money.decimal_mark,
        'thousand' => money.thousands_separator,
        'precision' => 2,
        'format' => money.currency.symbol_first ? "%s%v" : "%v%s",
        'subunit_symbol' => subunit_symbol(money.currency),
        'subunit_format' => subunit_symbol_first(money.currency) ? "%s%v" : "%v%s",
        'subunit_to_unit' => money.currency.subunit_to_unit,
        'iso_code' => money.currency_as_string
      }
    }
  end

  # Return whether the subunit symbol
  # should be first or not
  def self.subunit_symbol_first(currency)
    if currency.id == :cny
      return true
    else
      return false
    end
  end

  # Return the symbol of the
  # subunit
  def self.subunit_symbol(currency)
    if currency.subunit
      subunit_label = currency.subunit.downcase

      if subunit_label =~ /cent/i && currency.iso_code == 'EUR'
        subunit_label = 'ct'
      elsif subunit_label =~ /cent/i
        subunit_label = 'c'
      elsif subunit_label =~ /penny/i
        subunit_label = 'p'
      elsif subunit_label =~ /fen/i
        subunit_label = 20998.chr
      end

      return subunit_label
    else
      return nil
    end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
mno-enterprise-core-3.1.0 lib/accountingjs_serializer.rb
mno-enterprise-core-3.0.3 lib/accountingjs_serializer.rb
mno-enterprise-core-2.0.3 lib/accountingjs_serializer.rb
mno-enterprise-core-3.0.2 lib/accountingjs_serializer.rb
mno-enterprise-core-2.0.2 lib/accountingjs_serializer.rb
mno-enterprise-core-3.0.1 lib/accountingjs_serializer.rb
mno-enterprise-core-2.0.1 lib/accountingjs_serializer.rb
mno-enterprise-core-3.0.0 lib/accountingjs_serializer.rb
mno-enterprise-core-2.0.0 lib/accountingjs_serializer.rb