Class: RedBlocks::ComposedSet

Inherits:
Set
  • Object
show all
Defined in:
lib/red_blocks/composed_set.rb

Direct Known Subclasses

IntersectionSet, UnionSet

Constant Summary collapse

SCORE_AGGREGATORS =
[:sum, :min, :max]

Instance Attribute Summary collapse

Attributes inherited from Set

#weight

Instance Method Summary collapse

Methods inherited from Set

#disabled?, #expiration_time, #get, #ids, #key, key_pattern, #label, #size, #update_if_disabled!

Methods included from SetOptimizer

#unset

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_timeObject



34
35
36
# File 'lib/red_blocks/composed_set.rb', line 34

def cache_time
  @cache_time || super
end

#score_funcObject

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

#setsObject

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_suffixObject



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

#update!Object



28
29
30
31
32
# File 'lib/red_blocks/composed_set.rb', line 28

def update!
  disabled_sets.each(&:update!)
  compose_sets!
  RedBlocks.client.expire(key, expiration_time)
end