Sha256: 7d01a51043778cfc68babca88e56b7b1696e16ed5545d0ff522ab8456b073c97

Contents?: true

Size: 1.06 KB

Versions: 2

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

module Elasticity
  class SegmentedDocument < BaseDocument
    # Creates a new segment which behaves almost the same as a Document class dynamically
    # configured to access a segmented index.
    #
    # It creates a new class in runtime that inherits from your defined class, allowing
    # methods defined in your class to be callable from the dynamic class.
    def self.segment(segment_name)
      qn = segment_name.camelize

      klass = Class.new(self) do
        class_attribute :mapper, :segment_name
        IndexMapper.set_delegates(singleton_class, :mapper)

        def self.inspect
          "#{superclass.name}{\"#{segment_name}\"}"
        end

        def inspect
          ivars = instance_variables.map do |name|
            "#{name}=#{instance_variable_get(name).inspect}"
          end

          "#<#{self.class.inspect}:0x#{object_id.to_s(15)} #{ivars.join(" ")}>"
        end
      end

      klass.segment_name = segment_name
      klass.mapper = IndexMapper.new(klass, config.segment(segment_name))
      klass
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
es-elasticity-1.0.0 lib/elasticity/segmented_document.rb
es-elasticity-1.0.0.jhumphreys lib/elasticity/segmented_document.rb