Sha256: 1e17f45218fdfd439c32e4630283191ea37b5525b5d2474f61453f67398f125f

Contents?: true

Size: 1.13 KB

Versions: 4

Compression:

Stored size: 1.13 KB

Contents

module SimpleRedisOrm
  module Helpers
    module Serializer
      def serialize_value(value)
        return value if value.nil?

        MessagePack.pack(value)
      end

      def deserialize_value(value)
        return if value.nil?

        return deserialize_hash_value(value) if value.is_a?(Hash)

        MessagePack.unpack(value)
      end

      # rubocop:disable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
      def deserialize_hash_value(hash)
        return if hash.nil?

        hash.transform_values do |v|
          next v if v.nil?
          next v unless v.is_a?(String)
          next v unless v.encoding == Encoding::ASCII_8BIT

          v.strip.empty? ? v : MessagePack.unpack(v)
        rescue StandardError => e
          next without_invalid_characters(v) if e.instance_of?(ArgumentError)

          raise e
        end.deep_symbolize_keys
      end
      # rubocop:enable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity

      private

      def without_invalid_characters(text)
        return text unless text.is_a?(String)

        text.chars.select(&:valid_encoding?).join
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
simple-redis-orm-0.1.3 lib/simple-redis-orm/helpers/serializer.rb
simple-redis-orm-0.1.2 lib/simple-redis-orm/helpers/serializer.rb
simple-redis-orm-0.1.1 lib/simple-redis-orm/helpers/serializer.rb
simple-redis-orm-0.1.0 lib/simple-redis-orm/helpers/serializer.rb