Class: RoomPlanPointsSerializer

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

Overview

Note:

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

Serializes room planner polygon points for a JSON/JSONB column, coercing
every coordinate value to Float for the JavaScript room planner.

Class Method Summary collapse

Class Method Details

.cleanup_points_array(points = []) ⇒ Array<Hash{String => Float}>

Coerces every value in each point hash to Float, returning an empty
array for nil input.

Parameters:

  • points (Array<Hash>, nil) (defaults to: [])

    point hashes to coerce

Returns:

  • (Array<Hash{String => Float}>)

    the coerced points



29
30
31
32
33
34
35
36
# File 'app/serializers/room_plan_points_serializer.rb', line 29

def self.cleanup_points_array(points = [])
  return [] if points.nil?

  # Use to_f instead of to_d to avoid BigDecimal serializing as strings in JSON
  # BigDecimal.to_json returns a string to preserve precision, but the JavaScript
  # room planner expects numeric values for polygon calculations
  points.map { |h| h.transform_values(&:to_f) }
end

.dump(points) ⇒ Array<Hash{String => Float}>

Prepares the points array for database storage.

Parameters:

  • points (Array<Hash>, nil)

    point hashes to coerce

Returns:

  • (Array<Hash{String => Float}>)

    the coerced points



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

def self.dump(points)
  cleanup_points_array(points)
end

.load(points) ⇒ Array<Hash{String => Float}>

Restores the points array read from the database.

Parameters:

  • points (Array<Hash>, nil)

    point hashes to coerce

Returns:

  • (Array<Hash{String => Float}>)

    the coerced points



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

def self.load(points)
  cleanup_points_array(points)
end