Class: Kotoshu::Suggestions::Strategies::CompositeStrategy
- Inherits:
-
BaseStrategy
- Object
- BaseStrategy
- Kotoshu::Suggestions::Strategies::CompositeStrategy
- Defined in:
- lib/kotoshu/suggestions/strategies/composite_strategy.rb
Overview
Composite strategy that chains multiple suggestion strategies. Implements the Composite Pattern for extensible suggestion generation.
This is MORE OOP than Spylls which has a procedural suggestion pipeline. Here, strategies are proper objects that can be added/removed/reordered.
Instance Attribute Summary collapse
-
#strategies ⇒ Object
readonly
Returns the value of attribute strategies.
Attributes inherited from BaseStrategy
Class Method Summary collapse
-
.with_defaults(**config) ⇒ CompositeStrategy
Create a composite strategy with default algorithms.
Instance Method Summary collapse
-
#add(strategy) ⇒ CompositeStrategy
(also: #<<)
Add a strategy to the pipeline.
-
#any? ⇒ Boolean
Check if the composite has any strategies.
-
#applicable_strategies(context) ⇒ Array<BaseStrategy>
Get strategies that can handle the given context.
-
#clear ⇒ CompositeStrategy
Clear all strategies.
-
#each_strategy {|strategy| ... } ⇒ Enumerator
Iterate over strategies.
-
#generate(context) ⇒ SuggestionSet
Generate suggestions by delegating to all child strategies.
-
#handles?(context) ⇒ Boolean
Check if any strategy can handle the context.
-
#initialize(name:, strategies: [], **config) ⇒ CompositeStrategy
constructor
A new instance of CompositeStrategy.
-
#remove(strategy) ⇒ CompositeStrategy
Remove a strategy from the pipeline.
-
#size ⇒ Integer
(also: #count)
Get the number of strategies.
-
#sort_by_priority! ⇒ CompositeStrategy
Sort strategies by priority.
-
#to_s ⇒ String
(also: #inspect)
Convert to string.
Methods inherited from BaseStrategy
#calculate_ngram_similarity, #create_suggestion, #create_suggestion_set, #enabled?, #generate_ngrams, #get_config, #has_config?, #max_results, #priority
Constructor Details
#initialize(name:, strategies: [], **config) ⇒ CompositeStrategy
Returns a new instance of CompositeStrategy.
24 25 26 27 |
# File 'lib/kotoshu/suggestions/strategies/composite_strategy.rb', line 24 def initialize(name:, strategies: [], **config) @strategies = strategies super(name: name, **config) end |
Instance Attribute Details
#strategies ⇒ Object (readonly)
Returns the value of attribute strategies.
19 20 21 |
# File 'lib/kotoshu/suggestions/strategies/composite_strategy.rb', line 19 def strategies @strategies end |
Class Method Details
.with_defaults(**config) ⇒ CompositeStrategy
Create a composite strategy with default algorithms.
134 135 136 |
# File 'lib/kotoshu/suggestions/strategies/composite_strategy.rb', line 134 def self.with_defaults(**config) new(name: :default, **config) end |
Instance Method Details
#add(strategy) ⇒ CompositeStrategy Also known as: <<
Add a strategy to the pipeline.
33 34 35 36 |
# File 'lib/kotoshu/suggestions/strategies/composite_strategy.rb', line 33 def add(strategy) @strategies << strategy self end |
#any? ⇒ Boolean
Check if the composite has any strategies.
100 101 102 |
# File 'lib/kotoshu/suggestions/strategies/composite_strategy.rb', line 100 def any? @strategies.any? end |
#applicable_strategies(context) ⇒ Array<BaseStrategy>
Get strategies that can handle the given context.
60 61 62 |
# File 'lib/kotoshu/suggestions/strategies/composite_strategy.rb', line 60 def applicable_strategies(context) @strategies.select { |s| s.handles?(context) } end |
#clear ⇒ CompositeStrategy
Clear all strategies.
51 52 53 54 |
# File 'lib/kotoshu/suggestions/strategies/composite_strategy.rb', line 51 def clear @strategies.clear self end |
#each_strategy {|strategy| ... } ⇒ Enumerator
Iterate over strategies.
108 109 110 111 112 |
# File 'lib/kotoshu/suggestions/strategies/composite_strategy.rb', line 108 def each_strategy(&block) return enum_for(:each_strategy) unless block_given? @strategies.each(&block) end |
#generate(context) ⇒ SuggestionSet
Generate suggestions by delegating to all child strategies.
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/kotoshu/suggestions/strategies/composite_strategy.rb', line 68 def generate(context) # Create result set result = SuggestionSet.empty(max_size: context.max_results) # Process each applicable strategy applicable_strategies(context).each do |strategy| strategy_result = strategy.generate(context) result.merge!(strategy_result) end result end |
#handles?(context) ⇒ Boolean
Check if any strategy can handle the context.
85 86 87 |
# File 'lib/kotoshu/suggestions/strategies/composite_strategy.rb', line 85 def handles?(context) applicable_strategies(context).any? end |
#remove(strategy) ⇒ CompositeStrategy
Remove a strategy from the pipeline.
43 44 45 46 |
# File 'lib/kotoshu/suggestions/strategies/composite_strategy.rb', line 43 def remove(strategy) @strategies.delete(strategy) self end |
#size ⇒ Integer Also known as: count
Get the number of strategies.
92 93 94 |
# File 'lib/kotoshu/suggestions/strategies/composite_strategy.rb', line 92 def size @strategies.size end |
#sort_by_priority! ⇒ CompositeStrategy
Sort strategies by priority.
117 118 119 120 |
# File 'lib/kotoshu/suggestions/strategies/composite_strategy.rb', line 117 def sort_by_priority! @strategies.sort_by!(&:priority) self end |
#to_s ⇒ String Also known as: inspect
Convert to string.
125 126 127 |
# File 'lib/kotoshu/suggestions/strategies/composite_strategy.rb', line 125 def to_s "#{self.class.name}(name: #{@name}, strategies: #{@strategies.map(&:name).join(", ")})" end |