Class: BusinessCatalyst::CSV::CurrencyTransformer

Inherits:
Transformer
  • Object
show all
Defined in:
lib/business_catalyst/csv/transformers/currency_transformer.rb

Constant Summary collapse

BC_CURRENCY_REGEX =
/\A\w+\/\d/.freeze

Instance Attribute Summary collapse

Attributes inherited from Transformer

#input

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Transformer

transform

Constructor Details

#initialize(input, currency = nil) ⇒ CurrencyTransformer

Returns a new instance of CurrencyTransformer.



20
21
22
23
# File 'lib/business_catalyst/csv/transformers/currency_transformer.rb', line 20

def initialize(input, currency = nil)
  @currency = currency || self.class.default_currency
  super(input)
end

Instance Attribute Details

#currencyObject

Returns the value of attribute currency.



18
19
20
# File 'lib/business_catalyst/csv/transformers/currency_transformer.rb', line 18

def currency
  @currency
end

Class Method Details

.default_currencyObject



10
11
12
# File 'lib/business_catalyst/csv/transformers/currency_transformer.rb', line 10

def self.default_currency
  @default_currency || "US"
end

.default_currency=(currency) ⇒ Object



14
15
16
# File 'lib/business_catalyst/csv/transformers/currency_transformer.rb', line 14

def self.default_currency=(currency)
  @default_currency = currency
end

Instance Method Details

#is_bc_currency_string?(input) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/business_catalyst/csv/transformers/currency_transformer.rb', line 47

def is_bc_currency_string?(input)
  !!(input =~ BC_CURRENCY_REGEX)
end

#number_to_currency(input) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/business_catalyst/csv/transformers/currency_transformer.rb', line 34

def number_to_currency(input)
  if input
    input_s = input.kind_of?(BigDecimal) ? input.to_s('F') : input.to_s.strip
    if input_s != ""
      if is_bc_currency_string?(input_s)
        input_s
      else
        "#{currency}/#{input_s}"
      end
    end
  end
end

#transformObject



25
26
27
28
29
30
31
32
# File 'lib/business_catalyst/csv/transformers/currency_transformer.rb', line 25

def transform
  if input
    inputs = Array(input).map {|n| number_to_currency(n) }.compact
    if inputs.any?
      inputs.join(";")
    end
  end
end