Sha256: 0c9fa3444a42819c3ed639b4c5012b0fc19569ae38dd6b4f33d6e4fb970769b6

Contents?: true

Size: 882 Bytes

Versions: 6

Compression:

Stored size: 882 Bytes

Contents

# frozen_string_literal: true

class Serega
  class Convert
    module ConvertClassMethods
      def call(object, **opts)
        new(object, **opts).to_h
      end
    end

    module ConvertInstanceMethods
      attr_reader :object, :opts

      def initialize(object, **opts)
        @object = object
        @opts = opts
      end

      def to_h
        many? ? many(object) : one(object) || {}
      end

      private

      def many(objects)
        objects.map { |obj| one(obj) }
      end

      def one(object)
        self.class.serializer_class::ConvertItem.call(object, opts[:context], opts[:map])
      end

      def many?
        many = opts[:many]
        return many unless many.nil?

        object.is_a?(Enumerable)
      end
    end

    extend Serega::Helpers::SerializerClassHelper
    extend ConvertClassMethods
    include ConvertInstanceMethods
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
serega-0.1.5 lib/serega/convert.rb
serega-0.1.4 lib/serega/convert.rb
serega-0.1.3 lib/serega/convert.rb
serega-0.1.2 lib/serega/convert.rb
serega-0.1.1 lib/serega/convert.rb
serega-0.1.0 lib/serega/convert.rb