Class: Dato::Local::EntitiesRepo

Inherits:
Object
  • Object
show all
Defined in:
lib/dato/local/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.



10
11
12
13
# File 'lib/dato/local/entities_repo.rb', line 10

def initialize(*payloads)
  @entities = {}
  upsert_entities(*payloads)
end

Instance Attribute Details

#entitiesObject (readonly)

Returns the value of attribute entities.



8
9
10
# File 'lib/dato/local/entities_repo.rb', line 8

def entities
  @entities
end

Class Method Details

.payload_entities(payload) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/dato/local/entities_repo.rb', line 44

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

#destroy_entities(type, ids) ⇒ Object



23
24
25
26
27
# File 'lib/dato/local/entities_repo.rb', line 23

def destroy_entities(type, ids)
  ids.each do |id|
    entities.fetch(type, {}).delete(id)
  end
end

#destroy_item_type(id) ⇒ Object



29
30
31
32
# File 'lib/dato/local/entities_repo.rb', line 29

def destroy_item_type(id)
  entities.fetch("item", {}).delete_if { |_item_id, item| item.item_type.id == id }
  entities.fetch("item_type", {}).delete(id)
end

#find_entities_of_type(type) ⇒ Object



15
16
17
# File 'lib/dato/local/entities_repo.rb', line 15

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

#find_entity(type, id) ⇒ Object



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

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

#upsert_entities(*payloads) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/dato/local/entities_repo.rb', line 34

def upsert_entities(*payloads)
  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