Sha256: 70e2e91f6c211d2b8780c4e678ea80ffd631e2abca98109c439fc2f57d5190ea

Contents?: true

Size: 1.11 KB

Versions: 4

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

require "active_support/core_ext/module/delegation"

module PgSearch
  module Features
    class DMetaphone
      def initialize(query, options, columns, model, normalizer)
        dmetaphone_normalizer = Normalizer.new(normalizer)
        options = (options || {}).merge(dictionary: 'simple')
        @tsearch = TSearch.new(query, options, columns, model, dmetaphone_normalizer)
      end

      delegate :conditions, to: :tsearch

      delegate :rank, to: :tsearch

      private

      attr_reader :tsearch

      # Decorates a normalizer with dmetaphone processing.
      class Normalizer
        def initialize(normalizer_to_wrap)
          @normalizer_to_wrap = normalizer_to_wrap
        end

        def add_normalization(original_sql)
          otherwise_normalized_sql = Arel.sql(
            normalizer_to_wrap.add_normalization(original_sql)
          )

          Arel::Nodes::NamedFunction.new(
            "pg_search_dmetaphone",
            [otherwise_normalized_sql]
          ).to_sql
        end

        private

        attr_reader :normalizer_to_wrap
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
pg_search-2.3.6 lib/pg_search/features/dmetaphone.rb
pg_search-2.3.5 lib/pg_search/features/dmetaphone.rb
pg_search-2.3.4 lib/pg_search/features/dmetaphone.rb
pg_search-2.3.3 lib/pg_search/features/dmetaphone.rb