Class: RoomPlanPointsSerializer
- Inherits:
-
Object
- Object
- RoomPlanPointsSerializer
- 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
-
.cleanup_points_array(points = []) ⇒ Array<Hash{String => Float}>
Coerces every value in each point hash to
Float, returning an empty array for nil input. -
.dump(points) ⇒ Array<Hash{String => Float}>
Prepares the points array for database storage.
-
.load(points) ⇒ Array<Hash{String => Float}>
Restores the points array read from the database.
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.
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.
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.
20 21 22 |
# File 'app/serializers/room_plan_points_serializer.rb', line 20 def self.load(points) cleanup_points_array(points) end |