Class: Bronze::Transforms::Attributes::BigDecimalTransform

Inherits:
Bronze::Transform show all
Defined in:
lib/bronze/transforms/attributes/big_decimal_transform.rb

Overview

Transform class that normalizes a BigDecimal to a string representation.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.instanceBigDecimalTransform

Returns a memoized instance of BigDecimalTranform.

Returns:



12
13
14
# File 'lib/bronze/transforms/attributes/big_decimal_transform.rb', line 12

def self.instance
  @instance ||= new
end

Instance Method Details

#denormalize(value) ⇒ BigDecimal

Converts a normalized BigDecimal (a String) to a BigDecimal instance.

Parameters:

  • value (String)

    The normalized string.

Returns:

  • (BigDecimal)

    the denormalized instance.



21
22
23
24
25
26
27
# File 'lib/bronze/transforms/attributes/big_decimal_transform.rb', line 21

def denormalize(value)
  return nil if value.nil?

  BigDecimal(value)
rescue ArgumentError
  BigDecimal('0.0')
end

#normalize(value) ⇒ String

Converts a BigDecimal to a string representation.

Parameters:

  • value (BigDecimal)

    The BigDecimal to normalize.

Returns:

  • (String)

    the string representation.



34
35
36
37
38
# File 'lib/bronze/transforms/attributes/big_decimal_transform.rb', line 34

def normalize(value)
  return nil if value.nil?

  value.to_s
end