Sha256: 3fda98e5e1ce49ccdc46f8a5c6206ce8eb7e768599e98a8ef9a14fcdc3c043c4

Contents?: true

Size: 1.17 KB

Versions: 5

Compression:

Stored size: 1.17 KB

Contents

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

require 'zermelo/records/key'

module Zermelo
  module Associations
    class UniqueIndex

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

        @backend   = parent_klass.send(:backend)

        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_from).join(':') => id},
          indexer_to.key, {@backend.index_keys(@attribute_type, value_to).join(':') => id})
      end

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

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
zermelo-1.4.1 lib/zermelo/associations/unique_index.rb
zermelo-1.4.0 lib/zermelo/associations/unique_index.rb
zermelo-1.3.0 lib/zermelo/associations/unique_index.rb
zermelo-1.2.1 lib/zermelo/associations/unique_index.rb
zermelo-1.2.0 lib/zermelo/associations/unique_index.rb