Sha256: 000ad6285c10f4b5784ed4973d1bbceefb8aeb16554aebb13b377b2cbc0b4253

Contents?: true

Size: 1.41 KB

Versions: 4

Compression:

Stored size: 1.41 KB

Contents

module ReactiveRecord
  module LookupTables
    def initialize_lookup_tables
      @records = Hash.new { |hash, key| hash[key] = [] }
      @records_by_id = `{}`
      @records_by_vector = `{}`
      @records_by_object_id = `{}`
      @class_scopes = Hash.new { |hash, key| hash[key] = {} }
      @waiting_for_save = Hash.new { |hash, key| hash[key] = [] }
    end

    def class_scopes(model)
      @class_scopes[model.base_class]
    end

    def waiting_for_save(model)
      @waiting_for_save[model.base_class]
    end

    def wait_for_save(model, &block)
      @waiting_for_save[model.base_class] << block
    end

    def clear_waiting_for_save(model)
      @waiting_for_save[model.base_class] = []
    end

    def lookup_by_object_id(object_id)
      `#{@records_by_object_id}[#{object_id}]`.ar_instance
    end

    def set_object_id_lookup(record)
      `#{@records_by_object_id}[#{record.object_id}] = #{record}`
    end

    def lookup_by_id(model, id) # model and id
      `#{@records_by_id}[#{[model.base_class, id]}]` || nil
    end

    def set_id_lookup(record)
      `#{@records_by_id}[#{[record.model, record.id]}] = #{record}`
    end

    def lookup_by_vector(vector)
      `#{@records_by_vector}[#{vector}]` || nil
    end

    def set_vector_lookup(record, vector)
      record.vector = vector
      `delete #{@records_by_vector}[#{record.vector}]`
      `#{@records_by_vector}[#{vector}] = record`
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
hyper-model-1.0.alpha1.8 lib/reactive_record/active_record/reactive_record/lookup_tables.rb
hyper-model-1.0.alpha1.7 lib/reactive_record/active_record/reactive_record/lookup_tables.rb
hyper-model-1.0.alpha1.6 lib/reactive_record/active_record/reactive_record/lookup_tables.rb
hyper-model-1.0.alpha1.5 lib/reactive_record/active_record/reactive_record/lookup_tables.rb