Sha256: b09ef6b83ee67ad328a9c9b41a28327f85012e03dedc9cd7fd120f601076951d

Contents?: true

Size: 1.03 KB

Versions: 4

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true
require 'agave/local/json_api_entity'

module Agave
  module Local
    class EntitiesRepo
      attr_reader :entities

      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

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

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

      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
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
agave-client-0.1.3 lib/agave/local/entities_repo.rb
agave-client-0.1.2 lib/agave/local/entities_repo.rb
agave-client-0.1.1 lib/agave/local/entities_repo.rb
agave-client-0.1.0 lib/agave/local/entities_repo.rb