Class: Flatpack::Core::Unpacker

Inherits:
Object
  • Object
show all
Includes:
MapInitialize
Defined in:
lib/flatpack/core/unpacker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MapInitialize

#initialize, #set_properties

Instance Attribute Details

#entity_moduleObject

Returns the value of attribute entity_module.



9
10
11
# File 'lib/flatpack/core/unpacker.rb', line 9

def entity_module
  @entity_module
end

#verboseObject

Returns the value of attribute verbose.



9
10
11
# File 'lib/flatpack/core/unpacker.rb', line 9

def verbose
  @verbose
end

Instance Method Details

#unpack(json_map) ⇒ Object

Returns a Reified FlatPack entity from the given JSON data



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/flatpack/core/unpacker.rb', line 12

def unpack(json_map)
  @all_entities = {}

  if(@verbose)
    puts "** Deserializing entity from FlatPack **"
    puts JSON.pretty_generate(json_map)
  end

  # Make a first pass through the data to ensure stub entities exist
  uuid_to_data = {}
  (json_map['data'] || []).each do |entity_name, entities|
    if(entities.is_a?(Array))
      entities.each do |entity_data|
        uuid = entity_data['uuid']
        if(allocate_or_get_entity(entity_name, uuid))
          uuid_to_data[uuid] = entity_data
        end
      end
    end
  end

  # Then ingest
  uuid_to_data.each { |uuid, data| ingest_item(uuid, data) }

  # Return the unpacked entities corresponding to those in the value section
  value = json_map['value']
  value.is_a?(Array) ? value.map{|v| @all_entities[v]} : @all_entities[value]
end