Sha256: 4f7c1eb430e1a0f235623be11b182f37b2ac9fc90fb018a17fb7bc5f66e60bb3

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

module Cosmos

##
# Encapsulates the transformation of an aggregate packet into many simple packets
class AggregatePacketProcesser

  def initialize(mapper, transforms = {})
    @mapper = mapper
    @transforms = transforms
  end

  ##
  # Converts the aggregate packet into an array of simple packets.
  def unpack(packet, target, item) 
    values_map = @mapper.build_map(packet) # 
    _extract_map(values_map, target, item)
  end

  ##
  # Extract our internal BaseNameMap into an array of packets of type target/item, applying
  # transformations if applicable
  def _extract_map(map, target, item)
    map.extract_all.each_with_index.map do |m, i| 
     keys = m.keys
     pkt = System.telemetry.packet(target, item).clone 

     keys.reduce(pkt) do |p, key|
       value = m[key]
       transform_key = [target, item, key].join("-")

       if @transforms[transform_key]
         p.write(key, @transforms[transform_key].call(target, item, key, value, i))
       else
         p.write(key, value)
       end
       p
     end 
    end
  end
end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cosmos-unpacking_interface-1.0.0 lib/cosmos/unpacking_interface/aggregate_packet_processor.rb