Class: DatoDump::EntitiesRepo

Inherits:
Object
  • Object
show all
Defined in:
lib/dato_dump/entities_repo.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*payloads) ⇒ EntitiesRepo

Returns a new instance of EntitiesRepo.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/dato_dump/entities_repo.rb', line 7

def initialize(*payloads)
  @entities = {}

  payloads.each do |payload|
    EntitiesRepo.payload_entities(payload).each do |entity_payload|
      object = JsonApiEntity.new(entity_payload, self)
      @entities[object.type] ||= {}
      @entities[object.type][object.id] = object
    end
  end
end

Instance Attribute Details

#entitiesObject (readonly)

Returns the value of attribute entities.



5
6
7
# File 'lib/dato_dump/entities_repo.rb', line 5

def entities
  @entities
end

Class Method Details

.payload_entities(payload) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/dato_dump/entities_repo.rb', line 27

def self.payload_entities(payload)
  acc = []

  if payload[:data]
    acc = if payload[:data].is_a? Array
            acc + payload[:data]
          else
            acc + [payload[:data]]
          end
  end

  acc += payload[:included] if payload[:included]

  acc
end

Instance Method Details

#find_entities_of_type(type) ⇒ Object



19
20
21
# File 'lib/dato_dump/entities_repo.rb', line 19

def find_entities_of_type(type)
  entities.fetch(type, {}).values
end

#find_entity(type, id) ⇒ Object



23
24
25
# File 'lib/dato_dump/entities_repo.rb', line 23

def find_entity(type, id)
  entities.fetch(type, {}).fetch(id, nil)
end