Class: Kotoshu::Suggestions::Pipeline
- Inherits:
-
Object
- Object
- Kotoshu::Suggestions::Pipeline
- Defined in:
- lib/kotoshu/suggestions/pipeline.rb
Overview
Pipeline for composable suggestion strategies.
Allows chaining multiple suggestion strategies that execute in sequence, with optional early termination when a stage produces no results.
Instance Attribute Summary collapse
-
#stages ⇒ Array<Symbol>
readonly
Ordered stage names.
Instance Method Summary collapse
-
#add(stage_name) ⇒ Pipeline
Add a stage to the pipeline.
-
#clear ⇒ Pipeline
Clear all stages.
-
#clone ⇒ Pipeline
Clone the pipeline.
-
#execute(context, strategies = nil, early_termination: false) ⇒ SuggestionSet
Execute strategies through the pipeline.
-
#has_stage?(stage_name) ⇒ Boolean
Check if pipeline has a stage.
-
#initialize {|pipeline| ... } ⇒ Pipeline
constructor
Create a new pipeline.
-
#remove(stage_name) ⇒ Pipeline
Remove a stage from the pipeline.
Constructor Details
#initialize {|pipeline| ... } ⇒ Pipeline
Create a new pipeline.
33 34 35 36 |
# File 'lib/kotoshu/suggestions/pipeline.rb', line 33 def initialize @stages = [] yield self if block_given? end |
Instance Attribute Details
#stages ⇒ Array<Symbol> (readonly)
Returns Ordered stage names.
21 22 23 |
# File 'lib/kotoshu/suggestions/pipeline.rb', line 21 def stages @stages end |
Instance Method Details
#add(stage_name) ⇒ Pipeline
Add a stage to the pipeline.
45 46 47 48 |
# File 'lib/kotoshu/suggestions/pipeline.rb', line 45 def add(stage_name) @stages << stage_name self end |
#clear ⇒ Pipeline
Clear all stages.
111 112 113 114 |
# File 'lib/kotoshu/suggestions/pipeline.rb', line 111 def clear @stages.clear self end |
#clone ⇒ Pipeline
Clone the pipeline.
119 120 121 |
# File 'lib/kotoshu/suggestions/pipeline.rb', line 119 def clone self.class.new.tap { |p| @stages.each { |s| p.add(s) } } end |
#execute(context, strategies = nil, early_termination: false) ⇒ SuggestionSet
Execute strategies through the pipeline.
Strategies are executed in sequence. If a strategy returns an empty SuggestionSet, subsequent strategies are still executed unless early_termination is enabled.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/kotoshu/suggestions/pipeline.rb', line 76 def execute(context, strategies = nil, early_termination: false) combined = SuggestionSet.empty @stages.each do |stage_name| strategy = if strategies.is_a?(Hash) strategies[stage_name] else strategies end next unless strategy result = strategy.generate(context) # Combine results combined = combine_results(combined, result) # Early termination on empty result break if early_termination && result.empty? end combined end |
#has_stage?(stage_name) ⇒ Boolean
Check if pipeline has a stage.
104 105 106 |
# File 'lib/kotoshu/suggestions/pipeline.rb', line 104 def has_stage?(stage_name) @stages.include?(stage_name) end |
#remove(stage_name) ⇒ Pipeline
Remove a stage from the pipeline.
57 58 59 60 |
# File 'lib/kotoshu/suggestions/pipeline.rb', line 57 def remove(stage_name) @stages.delete(stage_name) self end |