Class: BusinessCatalyst::CSV::CurrencyTransformer
- Inherits:
-
Transformer
- Object
- Transformer
- BusinessCatalyst::CSV::CurrencyTransformer
- Defined in:
- lib/business_catalyst/csv/transformers/currency_transformer.rb
Constant Summary collapse
- BC_CURRENCY_REGEX =
/\A\w+\/\d/.freeze
Instance Attribute Summary collapse
-
#currency ⇒ Object
Returns the value of attribute currency.
Attributes inherited from Transformer
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(input, currency = nil) ⇒ CurrencyTransformer
constructor
A new instance of CurrencyTransformer.
- #is_bc_currency_string?(input) ⇒ Boolean
- #number_to_currency(input) ⇒ Object
- #transform ⇒ Object
Methods inherited from Transformer
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
#currency ⇒ Object
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_currency ⇒ Object
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
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 |
#transform ⇒ Object
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 |