Sha256: 49e16b9cecc54ede9c8c89ef06aadfdf23ef168e69ef739e48002cee92eae997

Contents?: true

Size: 837 Bytes

Versions: 8

Compression:

Stored size: 837 Bytes

Contents

module JsonApiClient
  module Associations
    class BaseAssociation
      attr_reader :attr_name, :klass, :options
      def initialize(attr_name, klass, options = {})
        @attr_name = attr_name
        @klass = klass
        @options = options
      end

      def association_class
        @association_class ||= Utils.compute_type(klass, options.fetch(:class_name) do
          attr_name.to_s.classify
        end)
      end

      def data(url)
        from_result_set(association_class.requestor.linked(url))
      end

      def from_result_set(result_set)
        result_set.to_a
      end

      def load_records(data)
        data.map do |d|
          record_class = Utils.compute_type(klass, klass.key_formatter.unformat(d["type"]).classify)
          record_class.load id: d["id"]
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
json_api_client-1.23.0 lib/json_api_client/associations/base_association.rb
json_api_client-1.22.0 lib/json_api_client/associations/base_association.rb
json_api_client-1.21.1 lib/json_api_client/associations/base_association.rb
json_api_client-1.21.0 lib/json_api_client/associations/base_association.rb
json_api_client-1.20.0 lib/json_api_client/associations/base_association.rb
json_api_client-1.19.0 lib/json_api_client/associations/base_association.rb
carwow-json_api_client-1.19.0 lib/json_api_client/associations/base_association.rb
json_api_client-1.18.0 lib/json_api_client/associations/base_association.rb