Sha256: 76b8083d814e41fbcf03e4088372085543045b97a01d3a7ea2f32c173038b857

Contents?: true

Size: 1.88 KB

Versions: 8

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

class ThinkingSphinx::RealTime::Index < Riddle::Configuration::RealtimeIndex
  include ThinkingSphinx::Core::Index

  attr_writer :fields, :attributes, :conditions, :scope

  def initialize(reference, options = {})
    @fields     = []
    @attributes = []
    @conditions = []

    super reference, options

    Template.new(self).apply
  end

  def add_attribute(attribute)
    @attributes.delete_if { |existing| existing.name == attribute.name }

    @attributes << attribute
  end

  def add_field(field)
    @fields.delete_if { |existing| existing.name == field.name }

    @fields << field
  end

  def attributes
    interpret_definition!

    @attributes
  end

  def conditions
    interpret_definition!

    @conditions
  end

  def facets
    properties.select(&:facet?)
  end

  def fields
    interpret_definition!

    @fields
  end

  def scope
    @scope.nil? ? model : @scope.call
  end

  def unique_attribute_names
    attributes.collect(&:name)
  end

  private

  def append_unique_attribute(collection, attribute)
    collection << attribute.name unless collection.include?(attribute.name)
  end

  def collection_for(attribute)
    case attribute.type
    when :integer, :boolean, :timestamp
      attribute.multi? ? @rt_attr_multi : @rt_attr_uint
    when :string
      @rt_attr_string
    when :float
      @rt_attr_float
    when :bigint
      attribute.multi? ? @rt_attr_multi_64 : @rt_attr_bigint
    when :json
      @rt_attr_json
    else
      raise "Unknown attribute type '#{attribute.type}'"
    end
  end

  def interpreter
    ThinkingSphinx::RealTime::Interpreter
  end

  def pre_render
    super

    @rt_field = fields.collect &:name

    attributes.each do |attribute|
      append_unique_attribute collection_for(attribute), attribute
    end
  end

  def properties
    fields + attributes
  end
end

require 'thinking_sphinx/real_time/index/template'

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
thinking-sphinx-5.6.0 lib/thinking_sphinx/real_time/index.rb
thinking-sphinx-5.5.1 lib/thinking_sphinx/real_time/index.rb
thinking-sphinx-5.5.0 lib/thinking_sphinx/real_time/index.rb
thinking-sphinx-5.4.0 lib/thinking_sphinx/real_time/index.rb
thinking-sphinx-5.3.0 lib/thinking_sphinx/real_time/index.rb
thinking-sphinx-5.2.1 lib/thinking_sphinx/real_time/index.rb
thinking-sphinx-5.2.0 lib/thinking_sphinx/real_time/index.rb
thinking-sphinx-5.1.0 lib/thinking_sphinx/real_time/index.rb