Class: HeatLossSerializer

Inherits:
Object
  • Object
show all
Defined in:
app/serializers/heat_loss_serializer.rb

Overview

Note:

Only use this with a json or jsonb back end column.

Serializes a heat-loss calculation result hash for a JSON/JSONB column,
coercing temperature ranges and per-object heat-loss values to BigDecimal.

Class Method Summary collapse

Class Method Details

.cleanup_hash(hash) ⇒ Hash

Coerces avg_temperature_ranges range bounds and hlc_by_object heat-loss
values to BigDecimal, returning an empty hash for blank input.

Parameters:

  • hash (Hash, nil)

    the heat-loss result to clean up

Returns:

  • (Hash)

    the cleaned-up hash



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/serializers/heat_loss_serializer.rb', line 29

def self.cleanup_hash(hash)
  return {} if hash.blank?

  if hash['avg_temperature_ranges'].present?
    hash['avg_temperature_ranges'] = hash['avg_temperature_ranges'].map do |v|
      v['range'] = v['range'].map(&:to_d)
      v
    end
  end
  if hash['hlc_by_object'].present?
    hash['hlc_by_object'] = hash['hlc_by_object'].map do |v|
      v['heat_loss'] = v['heat_loss'].to_d
      v
    end
  end
  hash
end

.dump(hash) ⇒ Hash

Prepares a heat-loss hash for database storage.

Parameters:

  • hash (Hash, nil)

    the heat-loss result to store

Returns:

  • (Hash)

    the cleaned-up hash



12
13
14
# File 'app/serializers/heat_loss_serializer.rb', line 12

def self.dump(hash)
  cleanup_hash(hash)
end

.load(hash) ⇒ Hash

Restores a heat-loss hash read from the database.

Parameters:

  • hash (Hash, nil)

    the stored heat-loss result

Returns:

  • (Hash)

    the cleaned-up hash



20
21
22
# File 'app/serializers/heat_loss_serializer.rb', line 20

def self.load(hash)
  cleanup_hash(hash)
end