Class: RailsPanda::Search::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_panda_search/config.rb

Constant Summary collapse

STRATEGY_REGISTRY =

Maps strategy names to their implementation classes. Register new strategies here or via Config#register_strategy.

{
  ngram: "RailsPanda::Search::Strategies::Ngram::NgramStrategy"
}.freeze
DEFAULT_TABLE_PREFIX =
"rails_panda_search"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



18
19
20
21
22
23
24
25
# File 'lib/rails_panda_search/config.rb', line 18

def initialize
  @strategies = [:ngram]
  @strategy_options = {}
  @custom_strategies = {}

  @ngram_size = 3
  @ngram_entries_table_name = "#{DEFAULT_TABLE_PREFIX}_ngram_entries"
end

Instance Attribute Details

#ngram_entries_table_nameObject

Returns the value of attribute ngram_entries_table_name.



8
9
10
# File 'lib/rails_panda_search/config.rb', line 8

def ngram_entries_table_name
  @ngram_entries_table_name
end

#ngram_sizeObject

Returns the value of attribute ngram_size.



8
9
10
# File 'lib/rails_panda_search/config.rb', line 8

def ngram_size
  @ngram_size
end

#strategiesObject



27
28
29
# File 'lib/rails_panda_search/config.rb', line 27

def strategies
  (@strategies.map(&:to_sym) | @custom_strategies.keys)
end

#strategy_optionsObject

Returns the value of attribute strategy_options.



7
8
9
# File 'lib/rails_panda_search/config.rb', line 7

def strategy_options
  @strategy_options
end

Instance Method Details

#active_strategy_classesObject

Returns implementation classes for all enabled strategies.



62
63
64
# File 'lib/rails_panda_search/config.rb', line 62

def active_strategy_classes
  strategies.map { |name| strategy_class_for(name) }
end

#register_strategy(name, klass) ⇒ Object

Register a custom strategy implementation. The class must be a subclass of Strategies::Base.

config.register_strategy(:elasticsearch, MyElasticsearchStrategy)


40
41
42
43
44
45
46
# File 'lib/rails_panda_search/config.rb', line 40

def register_strategy(name, klass)
  unless klass.is_a?(Class) && klass < Strategies::Base
    raise ArgumentError, "#{klass} must be a subclass of RailsPanda::Search::Strategies::Base"
  end

  @custom_strategies[name.to_sym] = klass
end

#strategy_class_for(name) ⇒ Object

Returns the implementation class for a given strategy name.



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rails_panda_search/config.rb', line 49

def strategy_class_for(name)
  klass = @custom_strategies[name.to_sym]
  return klass if klass

  class_name = STRATEGY_REGISTRY[name.to_sym]
  if class_name
    class_name.constantize
  else
    raise Error, "Unknown strategy: #{name}. Register it with config.register_strategy."
  end
end

#strategy_enabled?(name) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/rails_panda_search/config.rb', line 31

def strategy_enabled?(name)
  strategies.include?(name.to_sym)
end