Module: AccountingjsSerializer

Defined in:
lib/accountingjs_serializer.rb

Class Method Summary collapse

Class Method Details

.serialize(money) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/accountingjs_serializer.rb', line 3

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.to_s
    }
  }
end

.subunit_symbol(currency) ⇒ Object

Return the symbol of the subunit



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/accountingjs_serializer.rb', line 32

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

.subunit_symbol_first(currency) ⇒ Object

Return whether the subunit symbol should be first or not



22
23
24
25
26
27
28
# File 'lib/accountingjs_serializer.rb', line 22

def self.subunit_symbol_first(currency)
  if currency.id == :cny
    return true
  else
    return false
  end
end