Sha256: ac7e1701dff340cf673229f8b95187b0c1b373e9c37f53ca3a0f0b7af9276af7

Contents?: true

Size: 1004 Bytes

Versions: 17

Compression:

Stored size: 1004 Bytes

Contents

# frozen_string_literal: true

class ThinkingSphinx::Excerpter
  DefaultOptions = {
    :before_match    => '<span class="match">',
    :after_match     => '</span>',
    :chunk_separator => ' &#8230; ' # ellipsis
  }

  attr_accessor :index, :words, :options

  def initialize(index, words, options = {})
    @index, @words = index, words
    @options = DefaultOptions.merge(options)
    @words = @options.delete(:words) if @options[:words]
  end

  def excerpt!(text)
    result = ThinkingSphinx::Connection.take do |connection|
      query = statement_for text
      ThinkingSphinx::Logger.log :query, query do
        connection.execute(query).first['snippet']
      end
    end

    encoded? ? result : ThinkingSphinx::UTF8.encode(result)
  end

  private

  def statement_for(text)
    Riddle::Query.snippets(text, index, words, options)
  end

  def encoded?
    ThinkingSphinx::Configuration.instance.settings['utf8'].nil? ||
    ThinkingSphinx::Configuration.instance.settings['utf8']
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
thinking-sphinx-5.6.0 lib/thinking_sphinx/excerpter.rb
thinking-sphinx-5.5.1 lib/thinking_sphinx/excerpter.rb
thinking-sphinx-5.5.0 lib/thinking_sphinx/excerpter.rb
thinking-sphinx-5.4.0 lib/thinking_sphinx/excerpter.rb
thinking-sphinx-5.3.0 lib/thinking_sphinx/excerpter.rb
thinking-sphinx-5.2.1 lib/thinking_sphinx/excerpter.rb
thinking-sphinx-5.2.0 lib/thinking_sphinx/excerpter.rb
thinking-sphinx-5.1.0 lib/thinking_sphinx/excerpter.rb
thinking-sphinx-5.0.0 lib/thinking_sphinx/excerpter.rb
thinking-sphinx-4.4.1 lib/thinking_sphinx/excerpter.rb
thinking-sphinx-4.4.0 lib/thinking_sphinx/excerpter.rb
thinking-sphinx-4.3.2 lib/thinking_sphinx/excerpter.rb
thinking-sphinx-4.3.1 lib/thinking_sphinx/excerpter.rb
thinking-sphinx-4.3.0 lib/thinking_sphinx/excerpter.rb
thinking-sphinx-4.2.0 lib/thinking_sphinx/excerpter.rb
thinking-sphinx-4.1.0 lib/thinking_sphinx/excerpter.rb
thinking-sphinx-4.0.0 lib/thinking_sphinx/excerpter.rb