Sha256: 1a8c5ecaafc13ce622646adb5f5adbb4fb66b90cbbe5a253e4e06d458d73e66a

Contents?: true

Size: 776 Bytes

Versions: 15

Compression:

Stored size: 776 Bytes

Contents

# frozen_string_literal: true

class StringHack
  class FastIdList
    def self.from_array(array)
      new(",#{array.join(",")},")
    end

    def self.from_string(string)
      new(string)
    end

    def initialize(packed)
      @packed = packed
    end

    def include?(id)
      @packed.include?(",#{id},")
    end

    def to_s
      @packed
    end
  end

  def initialize
    @oj_options = { mode: :compat }
  end

  def encode(hash)
    if user_ids = hash["user_ids"]
      hash["user_ids"] = FastIdList.from_array(user_ids).to_s
    end

    ::Oj.dump(hash, @oj_options)
  end

  def decode(payload)
    result = ::Oj.load(payload, @oj_options)

    if str = result["user_ids"]
      result["user_ids"] = FastIdList.from_string(str)
    end

    result
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
message_bus-4.3.8 bench/codecs/string_hack.rb
message_bus-4.3.7 bench/codecs/string_hack.rb
message_bus-4.3.6 bench/codecs/string_hack.rb
message_bus-4.3.4 bench/codecs/string_hack.rb
message_bus-4.3.3 bench/codecs/string_hack.rb
message_bus-4.3.2 bench/codecs/string_hack.rb
message_bus-4.3.1 bench/codecs/string_hack.rb
message_bus-4.3.0 bench/codecs/string_hack.rb
message_bus-4.2.0 bench/codecs/string_hack.rb
message_bus-4.1.0 bench/codecs/string_hack.rb
message_bus-4.0.0 bench/codecs/string_hack.rb
message_bus-3.4.0 bench/codecs/string_hack.rb
message_bus-3.3.8 bench/codecs/string_hack.rb
message_bus-3.3.7 bench/codecs/string_hack.rb
message_bus-3.3.6 bench/codecs/string_hack.rb