Skip to content

Commit 7ecfad1

Browse files
Merge pull request #67 from epimorphics/66-handle-picking-most-specific-class
Introduce test class hierarchy and filter out superclasses
2 parents cd7554d + 26d6839 commit 7ecfad1

6 files changed

Lines changed: 29 additions & 7 deletions

File tree

.github/workflows/lint-app.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jobs:
66
lint:
77
runs-on: ubuntu-latest
88
steps:
9-
- uses: actions/checkout@v2
9+
- uses: actions/checkout@v4
1010
- name: Set up Ruby
1111
uses: ruby/setup-ruby@v1
1212
with:

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes will be documented in this file.
44

5+
## 1.4.0 - 2026-03-04
6+
7+
- (AlexT) When finding wrapper type, select the most specific type if more are available
8+
59
## 1.3.0 - 2025-01-16
610

711
- (AlexT) Add template endpoints https://github.com/epimorphics/sapi-client-ruby/pull/64

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
sapi-client-ruby (1.3.0)
4+
sapi-client-ruby (1.4.0)
55
faraday_middleware (~> 1.0.0)
66
i18n (~> 1.5)
77

lib/sapi_client/resource_wrapper.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,22 @@ def self.default_resource_wrapper_type
3030
# Find the first wrapper type for which there is an existing
3131
# class constant with the same name. If no such value is
3232
# found, return the default resource wrapper
33+
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
3334
def self.find_wrapper_type(types)
34-
Array(types).each do |type|
35-
wrapper = wrapper_class_constant(de_uri(type))
36-
return wrapper if wrapper
35+
all_wrapped = Array(types)
36+
.map { |type| wrapper_class_constant(de_uri(type)) }
37+
.filter { |wrapper| !wrapper.nil? }
38+
most_specific_wrapped = all_wrapped
39+
.filter do |t|
40+
all_wrapped.none? do |a|
41+
t != a && a.respond_to?(:ancestors) && a.ancestors.include?(t)
42+
end
3743
end
44+
return most_specific_wrapped[0] unless most_specific_wrapped.empty?
3845

3946
default_resource_wrapper_type
4047
end
48+
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
4149

4250
# Return the wrapper class for the given resource. If the
4351
# `options` specifies the wrapper class, then use that.

lib/sapi_client/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module SapiClient
44
MAJOR = 1
5-
MINOR = 3
5+
MINOR = 4
66
FIX = 0
77
VERSION = "#{MAJOR}.#{MINOR}.#{FIX}"
88
end

test/sapi_client/resource_wrapper_test.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
require 'test_helper'
44
require 'sapi_client'
55

6-
class WombleResource
6+
class CommonResource
77
def initialize(_ignored); end
88
end
99

10+
class WombleResource < CommonResource
11+
end
12+
1013
module SapiClient
1114
class ResourceWrapperTest < Minitest::Test
1215
describe 'ResourceWrapper' do
@@ -42,6 +45,13 @@ class ResourceWrapperTest < Minitest::Test
4245
).must_equal(WombleResource)
4346
end
4447

48+
it 'should find the most specific matching wrapper type' do
49+
_(
50+
ResourceWrapper
51+
.find_wrapper_type(%i[CommonResource Wimbledon WombleResource Array])
52+
).must_equal(WombleResource)
53+
end
54+
4555
it 'should return nil if a wrapper cannot be found' do
4656
_(
4757
ResourceWrapper

0 commit comments

Comments
 (0)