Sha256: b6bb9b85fc4cce23c5eb4c67691184a912f207676f83e617b1fbf2b4c82c8567

Contents?: true

Size: 1.32 KB

Versions: 5

Compression:

Stored size: 1.32 KB

Contents

module JsonApiClient
  class IncludedData
    attr_reader :data

    def initialize(result_set, data)
      record_class = result_set.record_class
      included_set = data.map do |datum|
        type = datum["type"]
        klass = Utils.compute_type(record_class, record_class.key_formatter.unformat(type).singularize.classify)
        params = klass.parser.parameters_from_resource(datum)
        resource = klass.load(params)
        resource.last_result_set = result_set
        resource
      end

      included_set.concat(result_set) if record_class.search_included_in_result_set
      @data = included_set.group_by(&:type).inject({}) do |h, (type, resources)|
        h[type] = resources.index_by(&:id)
        h
      end
    end

    def data_for(method_name, definition)
      # If data is defined, pull the record from the included data
      return nil unless data = definition["data"]

      if data.is_a?(Array)
        # has_many link
        data.map do |link_def|
          record_for(link_def)
        end
      else
        # has_one link
        record_for(data)
      end
    end

    def has_link?(name)
      data.has_key?(name.to_s)
    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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
json_api_client-1.12.0 lib/json_api_client/included_data.rb
json_api_client-1.11.0 lib/json_api_client/included_data.rb
json_api_client-1.10.0 lib/json_api_client/included_data.rb
json_api_client-1.9.0 lib/json_api_client/included_data.rb
json_api_client-1.8.0 lib/json_api_client/included_data.rb