Sha256: f462d0f9d708907d635e865b01787fba18ca4baa46ae1b532b7b2191a99d1c2a

Contents?: true

Size: 1016 Bytes

Versions: 1

Compression:

Stored size: 1016 Bytes

Contents

module JsonApiClient
  module Linking
    class IncludedData
      attr_reader :data

      def initialize(record_class, data)
        grouped_data = data.group_by{|datum| datum["type"]}
        @data = grouped_data.inject({}) do |h, (type, records)|
          klass = Utils.compute_type(record_class, type.singularize.classify)
          h[type] = records.map{|datum| klass.new(datum)}.index_by(&:id)
          h
        end
      end

      def data_for(method_name, definition)
        linkage = definition["linkage"]
        if linkage.is_a?(Array)
          # has_many link
          linkage.map do |link_def|
            record_for(link_def)
          end
        else
          # has_one link
          record_for(linkage)
        end
      end

      def has_link?(name)
        data.has_key?(name)
      end

      private

      # should return a resource record of some type for this linked document
      def record_for(link_def)
        data[link_def["type"]][link_def["id"]]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
json_api_client-1.0.0.beta lib/json_api_client/linking/included_data.rb