Sha256: d011387449f703c05cdc0c8cdd9e185b6e62fe5b12b4ae0b1cdf5a67171437bd

Contents?: true

Size: 1.13 KB

Versions: 2

Compression:

Stored size: 1.13 KB

Contents

# NB index instances are all internal to zermelo, not user-accessible

module Zermelo
  module Associations
    class UniqueIndex

      def initialize(parent_klass, name)
        @parent_klass   = parent_klass
        @attribute_name = name

        @backend   = parent_klass.send(:backend)
        @class_key = parent_klass.send(:class_key)

        @indexers = {}

        parent_klass.send(:with_index_data, name.to_sym) do |data|
          @attribute_type = data.type
        end
      end

      def delete_id(id, value)
        @backend.delete(key, @backend.index_keys(@attribute_type, value).join(':'))
      end

      def add_id(id, value)
        @backend.add(key, @backend.index_keys(@attribute_type, value).join(':') => id)
      end

      def move_id(id, value_from, indexer_to, value_to)
        @backend.move(key, {@backend.index_keys(@attribute_type, value_to).join(':') => id}, indexer_to.key)
      end

      def key
        @indexer ||= Zermelo::Records::Key.new(
          :klass  => @class_key,
          :name   => "by_#{@attribute_name}",
          :type   => :hash,
          :object => :index
        )
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
zermelo-1.0.1 lib/zermelo/associations/unique_index.rb
zermelo-1.0.0 lib/zermelo/associations/unique_index.rb