Class: Decidim::Ai::StrategyRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/decidim/ai/strategy_registry.rb

Defined Under Namespace

Classes: StrategyAlreadyRegistered

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStrategyRegistry

Returns a new instance of StrategyRegistry.



11
12
13
# File 'lib/decidim/ai/strategy_registry.rb', line 11

def initialize
  @strategies = []
end

Instance Attribute Details

#strategiesObject (readonly)

Returns the value of attribute strategies.



9
10
11
# File 'lib/decidim/ai/strategy_registry.rb', line 9

def strategies
  @strategies
end

Instance Method Details

#for(name) ⇒ Object



27
28
29
# File 'lib/decidim/ai/strategy_registry.rb', line 27

def for(name)
  strategies.select { |k, _v| k.name == name }.first
end

#register_analyzer(name:, strategy:, options: {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/decidim/ai/strategy_registry.rb', line 15

def register_analyzer(name:, strategy:, options: {})
  if self.for(name).present?
    raise(
      StrategyAlreadyRegistered,
      "There is a strategy already registered with the name `:#{name}`"
    )
  end

  options = { name: }.merge(options)
  strategies << strategy.new(options)
end