Class: Prefab::ContextShapeAggregator

Inherits:
Object
  • Object
show all
Includes:
PeriodicSync
Defined in:
lib/prefab/context_shape_aggregator.rb

Constant Summary collapse

LOG =
Prefab::InternalLogger.new(self)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PeriodicSync

#instance_hash, #on_prepare_data, #pool, #post, #start_periodic_sync, #sync

Constructor Details

#initialize(client:, max_shapes:, sync_interval:) ⇒ ContextShapeAggregator

Returns a new instance of ContextShapeAggregator.



12
13
14
15
16
17
18
19
20
# File 'lib/prefab/context_shape_aggregator.rb', line 12

def initialize(client:, max_shapes:, sync_interval:)
  @max_shapes = max_shapes
  @client = client
  @name = 'context_shape_aggregator'

  @data = Concurrent::Set.new

  start_periodic_sync(sync_interval)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



10
11
12
# File 'lib/prefab/context_shape_aggregator.rb', line 10

def data
  @data
end

Instance Method Details

#prepare_dataObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/prefab/context_shape_aggregator.rb', line 32

def prepare_data
  duped = @data.dup
  @data.clear

  duped.inject({}) do |acc, (name, key, type)|
    acc[name] ||= {}
    acc[name][key] = type
    acc
  end
end

#push(context) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/prefab/context_shape_aggregator.rb', line 22

def push(context)
  return if @data.size >= @max_shapes

  context.contexts.each_pair do |name, name_context|
    name_context.to_h.each_pair do |key, value|
      @data.add [name, key, Prefab::ContextShape.field_type_number(value)]
    end
  end
end