Sha256: ccfe603c6edae3a00d3190e1420119cb730bceb3facd4da7abc11a3fc5f2534a

Contents?: true

Size: 1.72 KB

Versions: 5

Compression:

Stored size: 1.72 KB

Contents

# frozen_string_literal: true

require 'dato/local/json_api_meta'

module Dato
  module Local
    class JsonApiEntity
      attr_reader :payload, :data_source

      def initialize(payload, data_source)
        @payload = payload
        @data_source = data_source
      end

      def id
        @payload[:id]
      end

      def type
        @payload[:type]
      end

      def meta
        @meta ||= JsonApiMeta.new(@payload[:meta])
      end

      def ==(other)
        if other.is_a? JsonApiEntity
          id == other.id && type == other.type
        else
          false
        end
      end

      def to_s
        "#<JsonApiEntity id=#{id} type=#{type} payload=#{payload}>"
      end
      alias inspect to_s

      def [](key)
        attributes[key]
      end

      def respond_to_missing?(method, include_private = false)
        if attributes.key?(method) || relationships.key?(method)
          true
        else
          super
        end
      end

      private

      def attributes
        @payload.fetch(:attributes, {})
      end

      def relationships
        @payload.fetch(:relationships, {})
      end

      def dereference_linkage(linkage)
        if linkage.is_a? Array
          linkage.map do |item|
            data_source.find_entity(item[:type], item[:id])
          end
        elsif linkage.is_a? Hash
          data_source.find_entity(linkage[:type], linkage[:id])
        end
      end

      def method_missing(method, *arguments, &block)
        return super unless arguments.empty?

        if attributes.key?(method)
          attributes[method]
        elsif relationships.key?(method)
          dereference_linkage(relationships[method][:data])
        else
          super
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dato-0.6.15 lib/dato/local/json_api_entity.rb
dato-0.6.14 lib/dato/local/json_api_entity.rb
dato-0.6.12 lib/dato/local/json_api_entity.rb
dato-0.6.11 lib/dato/local/json_api_entity.rb
dato-0.6.10 lib/dato/local/json_api_entity.rb