Class: RedBlocks::ComposedSet
Direct Known Subclasses
Constant Summary collapse
- SCORE_AGGREGATORS =
[:sum, :min, :max]
Instance Attribute Summary collapse
- #cache_time ⇒ Object
-
#score_func ⇒ Object
Returns the value of attribute score_func.
-
#sets ⇒ Object
Returns the value of attribute sets.
Attributes inherited from Set
Instance Method Summary collapse
- #expression(id) ⇒ Object
-
#initialize(sets, score_func: :sum, cache_time: nil) ⇒ ComposedSet
constructor
A new instance of ComposedSet.
- #key_suffix ⇒ Object
- #update! ⇒ Object
Methods inherited from Set
#disabled?, #expiration_time, #get, #ids, #key, key_pattern, #label, #size, #update_if_disabled!
Methods included from SetOptimizer
Methods included from SetUtils
included, #joined_key, #normalize_entries, #validate_array_entry, #validate_entries!
Constructor Details
#initialize(sets, score_func: :sum, cache_time: nil) ⇒ ComposedSet
Returns a new instance of ComposedSet.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/red_blocks/composed_set.rb', line 8 def initialize(sets, score_func: :sum, cache_time: nil) if sets.size&.zero? # Accepts enumerator, which can return nil for size. raise ArgumentError.new("Passed sets are blank.") end unless SCORE_AGGREGATORS.include?(score_func) raise ArgumentError.new("`#{score_func.inspect}` is not valid aggregator. Avaiable aggregator is #{SCORE_AGGREGATORS.join(', ')}") end unless sets.all? { |set| set.is_a?(RedBlocks::Set) } raise TypeError.new("sets must be a Array<RedBlocks::Set>, but got the following list: #{sets.map(&:class).join(', ')}") end @sets = sets @score_func = score_func @cache_time = cache_time end |
Instance Attribute Details
#cache_time ⇒ Object
34 35 36 |
# File 'lib/red_blocks/composed_set.rb', line 34 def cache_time @cache_time || super end |
#score_func ⇒ Object
Returns the value of attribute score_func.
3 4 5 |
# File 'lib/red_blocks/composed_set.rb', line 3 def score_func @score_func end |
#sets ⇒ Object
Returns the value of attribute sets.
3 4 5 |
# File 'lib/red_blocks/composed_set.rb', line 3 def sets @sets end |
Instance Method Details
#expression(id) ⇒ Object
38 39 40 |
# File 'lib/red_blocks/composed_set.rb', line 38 def expression(id) ComposedExpression.new(key, operator: score_func, operands: sets.map {|s| s.expression(id)}, label: label, weight: weight) end |
#key_suffix ⇒ Object
24 25 26 |
# File 'lib/red_blocks/composed_set.rb', line 24 def key_suffix joined_key(@sets.map(&:key).sort, sep: '|', wrap: true) end |