Sha256: 9e4708e48500b900d8f21ef0c382a1539348850d0c87c6d0819c3e650f56f276

Contents?: true

Size: 1.87 KB

Versions: 7

Compression:

Stored size: 1.87 KB

Contents

module AgnosticBackend
  module Indexable

    class Config

      class Entry

        attr_reader :index_class,
                    :options

        def initialize(index_class:, indexable_class:, primary: true, **options)
          @index_class = index_class
          @indexable_class = indexable_class
          @primary = primary
          @options = options
        end

        def primary?
          @primary
        end

        def create_index
          @index_class.new(@indexable_class, primary: @primary, **@options)
        end
      end


      def self.indices
        @indices ||= {}
      end

      def self.configure_index(indexable_class, index_class, **options)
        indices[indexable_class.name] = [Entry.new(index_class: index_class,
                                                   indexable_class: indexable_class,
                                                   primary: true,
                                                   **options)]
      end

      def self.configure_secondary_index(indexable_class, index_class, **options)
        unless indices.has_key? indexable_class.name
          raise "No primary index exists for class #{indexable_class.name}"
        end
        indices[indexable_class.name] << Entry.new(index_class: index_class,
                                                   indexable_class: indexable_class,
                                                   primary: false,
                                                   **options)
      end

      def self.create_index_for(indexable_class)
        entry = indices[indexable_class.name].find(&:primary?)
        entry.try(:create_index)
      end

      def self.create_indices_for(indexable_class, include_primary: true)
        all = indices[indexable_class.name].map {|entry| entry.try(:create_index)}.compact
        include_primary ? all : all.reject(&:primary?)
      end

    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
agnostic_backend-1.0.4 lib/agnostic_backend/indexable/config.rb
agnostic_backend-1.0.3 lib/agnostic_backend/indexable/config.rb
agnostic_backend-1.0.2 lib/agnostic_backend/indexable/config.rb
agnostic_backend-1.0.1 lib/agnostic_backend/indexable/config.rb
agnostic_backend-1.0.0 lib/agnostic_backend/indexable/config.rb
agnostic_backend-0.9.9 lib/agnostic_backend/indexable/config.rb
agnostic_backend-0.9.8 lib/agnostic_backend/indexable/config.rb