<commit_before>require 'json'
require 'deepstream/constants'
require 'deepstream/helpers'

module Deepstream
  class Record
    attr_reader :name, :data, :version

    def initialize(client, name)
      @client = client
      @name = name
      @data, @version = nil
      @client.send_message(TOPIC::RECORD, ACTION::CREATEORREAD, @name)
    end

    def inspect
      "#{self.class} #{@name} #{@version} #{@data}"
    end

    def unsubscribe
      @client.send_message(TOPIC::RECORD, ACTION::UNSUBSCRIBE, name)
    end

    def delete
      @client.delete(@name)
    end

    def set(*args)
      if args.one?
        @data = args.first
        @client.send_message(TOPIC::RECORD, ACTION::UPDATE, @name, (@version += 1), @data.to_json) if @version
      elsif args.size == 2
        path, value = args
        set_path(@data, path, value)
        @client.send_message(TOPIC::RECORD, ACTION::PATCH, @name, (@version += 1), path, Helpers.to_deepstream_type(value)) if @version
      end
    end

    def read(version, data)
      update(version, data)
    end

    def patch(version, path, value)
      @version = version.to_i
      set_path(@data, path, Helpers.to_type(value))
    end

    def update(version, data)
      @version = version.to_i
      @data = JSON.parse(data)
    end

    def method_missing(name, *args)
      return @data.fetch(name.to_s, nil) if args.empty?
      raise(NoMethodError, name)
    end

    private

    def set_path(data, path, value)
      key, subkey = path.split('.', 2)
      if data.is_a?(Hash)
        subkey ? set_path(data.fetch(key), subkey, value) : data[key] = value
      elsif data.is_a?(Array)
        subkey ? set_path(data[key.to_i], subkey, value) : data[key.to_i] = value
      end
    end
  end
end
<commit_msg>initial record data should be a hash<commit_after>require 'json'
require 'deepstream/constants'
require 'deepstream/helpers'

module Deepstream
  class Record
    attr_reader :name, :data, :version

    def initialize(client, name)
      @client = client
      @name = name
      @data = {}
      @version = nil
      @client.send_message(TOPIC::RECORD, ACTION::CREATEORREAD, @name)
    end

    def inspect
      "#{self.class} #{@name} #{@version} #{@data}"
    end

    def unsubscribe
      @client.send_message(TOPIC::RECORD, ACTION::UNSUBSCRIBE, name)
    end

    def delete
      @client.delete(@name)
    end

    def set(*args)
      if args.one?
        @data = args.first
        @client.send_message(TOPIC::RECORD, ACTION::UPDATE, @name, (@version += 1), @data.to_json) if @version
      elsif args.size == 2
        path, value = args
        set_path(@data, path, value)
        @client.send_message(TOPIC::RECORD, ACTION::PATCH, @name, (@version += 1), path, Helpers.to_deepstream_type(value)) if @version
      end
    end

    def read(version, data)
      update(version, data)
    end

    def patch(version, path, value)
      @version = version.to_i
      set_path(@data, path, Helpers.to_type(value))
    end

    def update(version, data)
      @version = version.to_i
      @data = JSON.parse(data)
    end

    def method_missing(name, *args)
      return @data.fetch(name.to_s, nil) if args.empty?
      raise(NoMethodError, name)
    end

    private

    def set_path(data, path, value)
      key, subkey = path.split('.', 2)
      if data.is_a?(Hash)
        subkey ? set_path(data.fetch(key), subkey, value) : data[key] = value
      elsif data.is_a?(Array)
        subkey ? set_path(data[key.to_i], subkey, value) : data[key.to_i] = value
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.1273492286115007, 'fraction_numerical': 0.002244039270687237, 'mean_word_length': 2.7030114226375908, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2193209', 'n_tokens_mistral': 1218, 'n_tokens_neox': 1197, 'n_words': 292}