Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/lint-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
Expand Down
14 changes: 11 additions & 3 deletions lib/sapi_client/resource_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,22 @@ def self.default_resource_wrapper_type
# Find the first wrapper type for which there is an existing
# class constant with the same name. If no such value is
# found, return the default resource wrapper
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
def self.find_wrapper_type(types)
Array(types).each do |type|
wrapper = wrapper_class_constant(de_uri(type))
return wrapper if wrapper
all_wrapped = Array(types)
.map { |type| wrapper_class_constant(de_uri(type)) }
.filter { |wrapper| !wrapper.nil? }
most_specific_wrapped = all_wrapped
.filter do |t|
all_wrapped.none? do |a|
t != a && a.respond_to?(:ancestors) && a.ancestors.include?(t)
end
end
return most_specific_wrapped[0] unless most_specific_wrapped.empty?

default_resource_wrapper_type
end
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength

# Return the wrapper class for the given resource. If the
# `options` specifies the wrapper class, then use that.
Expand Down
12 changes: 11 additions & 1 deletion test/sapi_client/resource_wrapper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
require 'test_helper'
require 'sapi_client'

class WombleResource
class CommonResource
def initialize(_ignored); end
end

class WombleResource < CommonResource
end

module SapiClient
class ResourceWrapperTest < Minitest::Test
describe 'ResourceWrapper' do
Expand Down Expand Up @@ -42,6 +45,13 @@ class ResourceWrapperTest < Minitest::Test
).must_equal(WombleResource)
end

it 'should find the most specific matching wrapper type' do
_(
ResourceWrapper
.find_wrapper_type(%i[CommonResource Wimbledon WombleResource Array])
).must_equal(WombleResource)
end

it 'should return nil if a wrapper cannot be found' do
_(
ResourceWrapper
Expand Down