Sha256: 10c0e748190dd3420bc5217570f3b47ef5e34bac1c4a35d73ab18e768db84321

Contents?: true

Size: 892 Bytes

Versions: 3

Compression:

Stored size: 892 Bytes

Contents

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|
      connection.execute(statement_for(text)).first['snippet']
    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

3 entries across 3 versions & 1 rubygems

Version Path
thinking-sphinx-3.1.2 lib/thinking_sphinx/excerpter.rb
thinking-sphinx-3.1.1 lib/thinking_sphinx/excerpter.rb
thinking-sphinx-3.1.0 lib/thinking_sphinx/excerpter.rb