Class: ArrayDecimalSerializer
- Inherits:
-
Object
- Object
- ArrayDecimalSerializer
- Defined in:
- app/serializers/array_decimal_serializer.rb
Overview
Note:
Only use this with a json or jsonb back end column.
Serializes arrays of decimal values for storage in a JSON/JSONB column,
coercing every element (including nested arrays) to BigDecimal.
Class Method Summary collapse
-
.array_of_decimals(array) ⇒ Array<BigDecimal>+
Recursively coerces every element of the array to
BigDecimal. -
.dump(array) ⇒ Array<BigDecimal>+
Prepares an array for database storage.
-
.load(array) ⇒ Array<BigDecimal>+
Restores an array read from the database.
Class Method Details
.array_of_decimals(array) ⇒ Array<BigDecimal>+
Recursively coerces every element of the array to BigDecimal.
28 29 30 31 32 33 34 35 36 |
# File 'app/serializers/array_decimal_serializer.rb', line 28 def self.array_of_decimals(array) array.map do |v| if v.is_a?(Array) array_of_decimals(v) else v.to_d end end end |
.dump(array) ⇒ Array<BigDecimal>+
Prepares an array for database storage.
12 13 14 |
# File 'app/serializers/array_decimal_serializer.rb', line 12 def self.dump(array) array_of_decimals(array || []) end |
.load(array) ⇒ Array<BigDecimal>+
Restores an array read from the database.
20 21 22 |
# File 'app/serializers/array_decimal_serializer.rb', line 20 def self.load(array) array_of_decimals(array || []) end |