Class: Cosmos::BaseNameMap

Inherits:
Object
  • Object
show all
Defined in:
lib/cosmos/unpacking_interface/base_name_map.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(map, name) ⇒ BaseNameMap

map is a hash where keys are normalized item names (i.e. VALUE_A instead of VALUE_A_0) and values are arrays of all the values whose original keys (VALUE_A_x) mapped to that value. For example: { ‘VALUE_A’ => [1,2,3]}.

name is the name of the packet



13
14
15
16
# File 'lib/cosmos/unpacking_interface/base_name_map.rb', line 13

def initialize(map, name)
  @map = map 
  @name = name # packet name
end

Instance Attribute Details

#mapObject

Returns the value of attribute map.



5
6
7
# File 'lib/cosmos/unpacking_interface/base_name_map.rb', line 5

def map
  @map
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/cosmos/unpacking_interface/base_name_map.rb', line 5

def name
  @name
end

Instance Method Details

#_field_arity(k) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/cosmos/unpacking_interface/base_name_map.rb', line 33

def _field_arity(k)
  result = @map[k]
  if result.nil?
    0
  else
    result.length
  end
end

#_max_arityObject



28
29
30
31
# File 'lib/cosmos/unpacking_interface/base_name_map.rb', line 28

def _max_arity
  return 0 if @map.empty?
  @map.max_by {|k, v| v.length}.last.length
end

#extract_allObject

Expands map of keys to arrays of values into a single array containing hashes where each key maps to only a single value i.e. => [1,2] => [=> 1, => 2]



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cosmos/unpacking_interface/base_name_map.rb', line 46

def extract_all 
 max = _max_arity
 range = if(max > 0)
   [*0..(max - 1)]
 else
   [0]
 end

 range.map do |i|
   keys = @map.keys
   keys.reduce({}) do |acc, k|
     if(_field_arity(k) == 1)
       acc[k] = @map[k].first
     else
       acc[k] = @map[k][i]
     end
     acc
   end 
 end
end

#sizeObject

Number of simple packets contained within the aggregate packet



20
21
22
23
24
25
26
# File 'lib/cosmos/unpacking_interface/base_name_map.rb', line 20

def size 
  if @map.empty?
    return 0
  else 
    _max_arity
  end
end