Sha256: 43c69d29708d4dc6d2301b2435b2f9bd45f39adcfd5308e268683bc0793f6895

Contents?: true

Size: 1.05 KB

Versions: 11

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

module Shark
  module RSpec
    module Helpers
      module CacheHelper
        def find(type, id)
          objects.detect { |o| o['type'] == type && o['id'] == id }
        end

        def included_resources(object_or_objects, params)
          return [] unless params['include'].present?

          relationships = params['include'].split(',')
          included_objects = []

          to_array(object_or_objects).each do |object|
            relationships.each do |name|
              rdata = object.dig('relationships', name, 'data')
              to_array(rdata).each do |r|
                included_objects << find(r['type'], r['id'])
              end
            end
          end

          included_objects.compact
        end

        private

        def to_array(object_or_objects)
          case object_or_objects
          when Hash
            [object_or_objects]
          when Array
            object_or_objects
          else
            Array(object_or_objects)
          end
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
bima-shark-sdk-3.1.1 lib/shark/rspec/helpers/cache_helper.rb
bima-shark-sdk-2.5.0 lib/shark/rspec/helpers/cache_helper.rb
bima-shark-sdk-3.1.0 lib/shark/rspec/helpers/cache_helper.rb
bima-shark-sdk-3.0.0 lib/shark/rspec/helpers/cache_helper.rb
bima-shark-sdk-2.4.4 lib/shark/rspec/helpers/cache_helper.rb
bima-shark-sdk-2.4.3 lib/shark/rspec/helpers/cache_helper.rb
bima-shark-sdk-2.4.2 lib/shark/rspec/helpers/cache_helper.rb
bima-shark-sdk-2.4.1 lib/shark/rspec/helpers/cache_helper.rb
bima-shark-sdk-2.4.0 lib/shark/rspec/helpers/cache_helper.rb
bima-shark-sdk-2.3.1 lib/shark/rspec/helpers/cache_helper.rb
bima-shark-sdk-2.3.0 lib/shark/rspec/helpers/cache_helper.rb