lib/serega/object_serializer.rb in serega-0.6.1 vs lib/serega/object_serializer.rb in serega-0.7.0
- old
+ new
@@ -1,36 +1,49 @@
# frozen_string_literal: true
class Serega
+ #
+ # Low-level class that is used by more high-level SeregaSerializer
+ # to construct serialized to hash response
+ #
class SeregaObjectSerializer
- module SeregaObjectSerializerInstanceMethods
+ #
+ # SeregaObjectSerializer instance methods
+ #
+ module InstanceMethods
attr_reader :context, :points, :many, :opts
# @param context [Hash] Serialization context
# @param many [TrueClass|FalseClass] is object is enumerable
# @param points [Array<MapPoint>] Serialization points (attributes)
+ #
+ # @return [SeregaObjectSerializer] New SeregaObjectSerializer
def initialize(context:, points:, many: nil, **opts)
@context = context
@points = points
@many = many
@opts = opts
end
+ # Serializes object(s)
+ #
# @param object [Object] Serialized object
+ #
+ # @return [Hash, Array<Hash>] Serialized object(s)
def serialize(object)
+ return if object.nil?
+
array?(object, many) ? serialize_array(object) : serialize_object(object)
end
private
def serialize_array(object)
object.map { |obj| serialize_object(obj) }
end
def serialize_object(object)
- return unless object
-
points.each_with_object({}) do |point, container|
attach_value(object, point, container)
end
end
@@ -60,8 +73,8 @@
many.nil? ? object.is_a?(Enumerable) : many
end
end
extend Serega::SeregaHelpers::SerializerClassHelper
- include SeregaObjectSerializerInstanceMethods
+ include InstanceMethods
end
end