Sha256: 3e755a6a62962de11bc8c4d8359a33366d8e3017ddd02e2b1ada9c095134a869

Contents?: true

Size: 1.03 KB

Versions: 8

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

require 'dato/local/json_api_entity'

module Dato
  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

8 entries across 8 versions & 1 rubygems

Version Path
dato-0.6.18 lib/dato/local/entities_repo.rb
dato-0.6.17 lib/dato/local/entities_repo.rb
dato-0.6.16 lib/dato/local/entities_repo.rb
dato-0.6.15 lib/dato/local/entities_repo.rb
dato-0.6.14 lib/dato/local/entities_repo.rb
dato-0.6.12 lib/dato/local/entities_repo.rb
dato-0.6.11 lib/dato/local/entities_repo.rb
dato-0.6.10 lib/dato/local/entities_repo.rb