Class: Agave::Local::EntitiesRepo

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



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/agave/local/entities_repo.rb', line 9

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.



7
8
9
# File 'lib/agave/local/entities_repo.rb', line 7

def entities
  @entities
end

Class Method Details

.payload_entities(payload) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/agave/local/entities_repo.rb', line 29

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



21
22
23
# File 'lib/agave/local/entities_repo.rb', line 21

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

#find_entity(type, id) ⇒ Object



25
26
27
# File 'lib/agave/local/entities_repo.rb', line 25

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