Sha256: 56ae6795f786ff8462de3d7f93c5d913582fb90837e97e352be85906f3e37d97

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

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

      def conditions
        tsearch.conditions
      end

      def rank
        tsearch.rank
      end

      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

1 entries across 1 versions & 1 rubygems

Version Path
pg_search-2.1.4 lib/pg_search/features/dmetaphone.rb