<commit_before>require "rubygems"
require "redis"

module Ohm
  class Model
    ModelIsNew = Class.new(StandardError)

    @@attributes = Hash.new { |h,k| h[k] = [] }

    attr_accessor :id

    def self.attribute(name)
      attr_writer(name)
      attr_lazy_reader(name)
      attributes << name
    end

    def self.attr_lazy_reader(name)
      class_eval <<-EOS
        def #{name}
          @#{name} ||= db[key(#{name.inspect})]
        end
      EOS
    end

    def self.[](id)
      if db[key(self, id)]
        model = new
        model.id = id
        model
      end
    end

    def self.all
      db.set_members(key(self)).map do |id|
        self[id]
      end
    end

    def self.attributes
      @@attributes[self]
    end

    def self.next_id
      db.incr(key(self, "id"))
    end

    def new?
      ! self.id
    end

    def create
      self.id = self.class.next_id
      db.set_add(self.class.key(self.class), self.id)
      db[key] = true
      save
    end

    def save
      ensure_model_exists

      self.class.attributes.each do |att|
        db[key(att)] = send(att)
      end

      self
    end

  private

    def ensure_model_exists
      raise ModelIsNew if new?
    end

    def key(*args)
      self.class.key([self.class, id] + args)
    end

    def self.key(*args)
      args.join(":")<commit_msg>Factored out class name from key<commit_after>require "rubygems"
require "redis"

module Ohm
  class Model
    ModelIsNew = Class.new(StandardError)

    @@attributes = Hash.new { |hash, key| hash[key] = [] }

    attr_accessor :id

    def self.attribute(name)
      attr_writer(name)
      attr_lazy_reader(name)
      attributes << name
    end

    def self.attr_lazy_reader(name)
      class_eval <<-EOS
        def #{name}
          @#{name} ||= db[key(#{name.inspect})]
        end
      EOS
    end

    def self.[](id)
      if db[key(id)]
        model = new
        model.id = id
        model
      end
    end

    def self.all
      db.set_members(key).map do |id|
        self[id]
      end
    end

    def self.attributes
      @@attributes[self]
    end

    def self.next_id
      db.incr(key("id"))
    end

    def new?
      ! self.id
    end

    def create
      self.id = self.class.next_id
      db.set_add(self.class.key, self.id)
      db[key] = true
      save
    end

    def save
      verify_model_exists

      self.class.attributes.each do |att|
        db[key(att)] = send(att)
      end

      self
    end

  private

    def verify_model_exists
      raise ModelIsNew if new?
    end

    def key(*args)
      self.class.key(id, *args)
    end

    def self.key(*args)
      args.unshift(self).join(":")
{'fraction_non_alphanumeric': 0.11065573770491803, 'fraction_numerical': 0.0, 'mean_word_length': 1.791060291060291, 'pattern_counts': {'":': 2, '<': 11, '<?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': '5844711', 'n_tokens_mistral': 959, 'n_tokens_neox': 920, 'n_words': 212}