Skip to content

Commit 60230a6

Browse files
authored
Merge pull request #24 from Sage/ENG-2680
Support multiple data types for Swagger model attribute
2 parents 51d02ff + e9f5641 commit 60230a6

7 files changed

Lines changed: 122 additions & 18 deletions

File tree

.github/workflows/rspec.yml

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,32 @@ jobs:
99
- uses: actions/checkout@v2
1010
- uses: ruby/setup-ruby@v1
1111
with:
12-
ruby-version: 2.4
12+
ruby-version: 2.5
1313
bundler-cache: true
1414

1515
- name: Run tests
1616
run: bundle exec rspec
1717

18-
- name: Code Coverage
19-
uses: paambaati/codeclimate-action@v2.7.5
20-
env:
21-
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
18+
- name: 'Upload Coverage Report'
19+
uses: actions/upload-artifact@v4
2220
with:
23-
coverageLocations: |
24-
${{github.workspace}}/coverage/.resultset.json:simplecov
21+
name: coverage-report
22+
path: ./coverage
23+
24+
coverage:
25+
needs: [ test ]
26+
name: coverage
27+
runs-on: ubuntu-latest
28+
steps:
29+
- uses: actions/checkout@v4
30+
- name: Download Coverage Report
31+
uses: actions/download-artifact@v4
32+
with:
33+
name: coverage-report
34+
path: ./coverage
35+
- uses: paambaati/codeclimate-action@v9.0.0
36+
env:
37+
# Set CC_TEST_REPORTER_ID as secret of your repo
38+
CC_TEST_REPORTER_ID: ${{secrets.CC_TEST_REPORTER_ID}}
39+
with:
40+
debug: true

class_kit.gemspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ Gem::Specification.new do |spec|
1919
spec.require_paths = ['lib']
2020

2121
spec.add_development_dependency 'bundler', '~> 2'
22-
spec.add_development_dependency 'pry'
2322
spec.add_development_dependency 'rake', '~> 10.0'
2423
spec.add_development_dependency 'rspec'
25-
spec.add_development_dependency 'simplecov', '< 0.18.0'
24+
spec.add_development_dependency 'simplecov', ' ~> 0.22.0'
25+
spec.add_development_dependency 'simplecov_json_formatter'
2626

2727
spec.add_dependency 'hash_kit'
2828
spec.add_dependency 'json'

lib/class_kit/class_methods.rb

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ def attr_accessor_type(
33
name,
44
type: nil,
55
collection_type: nil,
6+
one_of: nil,
67
allow_nil: true,
78
default: nil,
89
auto_init: false,
@@ -17,6 +18,7 @@ def attr_accessor_type(
1718
attributes[name] = {
1819
name: name,
1920
type: type,
21+
one_of: one_of,
2022
collection_type: collection_type,
2123
allow_nil: allow_nil,
2224
default: default,
@@ -54,7 +56,28 @@ def attr_accessor_type(
5456
raise ClassKit::Exceptions::InvalidAttributeValueError, "Attribute: #{name}, must not be nil."
5557
end
5658

57-
# check if the value being set is not of the specified type and should attempt to parse the value
59+
if !cka[:one_of].nil? && !value.nil?
60+
parsed_value =
61+
if value == true || value == false
62+
value
63+
elsif(/(true|t|yes|y|1)$/i === value.to_s.downcase)
64+
true
65+
elsif (/(false|f|no|n|0)$/i === value.to_s.downcase)
66+
false
67+
end
68+
69+
if parsed_value != nil
70+
value = parsed_value
71+
else
72+
begin
73+
type = cka[:one_of].detect {|t| value.is_a?(t) }
74+
value = ClassKit::ValueHelper.instance.parse(type: type, value: value)
75+
rescue => e
76+
raise ClassKit::Exceptions::InvalidAttributeValueError, "Attribute: #{name}, must be of type: #{type}. Error: #{e}"
77+
end
78+
end
79+
end
80+
5881
if !cka[:type].nil? && !value.nil? && (cka[:type] == :bool || !value.is_a?(cka[:type]))
5982
begin
6083
value = ClassKit::ValueHelper.instance.parse(type: cka[:type], value: value)

spec/class_kit/class_methods_spec.rb

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
end
2121

2222
context 'when a child entity is empty' do
23-
it 'class_kit_attributes are avaiable' do
23+
it 'class_kit_attributes are available' do
2424
expect{ empty_child_entity.base1 }.not_to raise_error
2525
end
2626
end
@@ -35,13 +35,15 @@
3535
expect(test_entity.int).to be 10
3636
end
3737
end
38+
3839
context 'when an attribute is NOT allowed to be nil' do
3940
context 'when attempting to set the value to nil' do
4041
it 'should raise an invalid attribute value error' do
4142
expect{ test_entity.int = nil }.to raise_error(ClassKit::Exceptions::InvalidAttributeValueError)
4243
end
4344
end
4445
end
46+
4547
context 'when an attribute is allowed to be nil' do
4648
it 'should not raise an error when setting to nil' do
4749
expect{ test_entity.int_nil = nil }.not_to raise_error
@@ -57,13 +59,15 @@
5759
expect(test_entity.int).to eq(20)
5860
end
5961
end
62+
6063
context 'from a string' do
6164
context 'that can be parsed' do
6265
it 'should parse and set the value' do
6366
expect{ test_entity.int = '20' }.not_to raise_error
6467
expect(test_entity.int).to eq(20)
6568
end
6669
end
70+
6771
context 'that can NOT be parsed' do
6872
it 'should raise an error' do
6973
expect{ test_entity.int = 'ABC' }.to raise_error(ClassKit::Exceptions::InvalidAttributeValueError)
@@ -81,13 +85,15 @@
8185
expect(test_entity.float).to eq(0.05)
8286
end
8387
end
88+
8489
context 'from a string' do
8590
context 'that can be parsed' do
8691
it 'should parse and set the value' do
8792
expect{ test_entity.float = '0.05' }.not_to raise_error
8893
expect(test_entity.float).to eq(0.05)
8994
end
9095
end
96+
9197
context 'that can NOT be parsed' do
9298
it 'should raise an error' do
9399
expect{ test_entity.float = 'ABC' }.to raise_error(ClassKit::Exceptions::InvalidAttributeValueError)
@@ -106,6 +112,7 @@
106112
expect(test_entity.date).to eq(date)
107113
end
108114
end
115+
109116
context 'from a string' do
110117
context 'that can be parsed' do
111118
let(:date) { Date.today }
@@ -114,6 +121,7 @@
114121
expect(test_entity.date).to eq(date)
115122
end
116123
end
124+
117125
context 'that can NOT be parsed' do
118126
it 'should raise an error' do
119127
expect{ test_entity.date = 'ABC' }.to raise_error(ClassKit::Exceptions::InvalidAttributeValueError)
@@ -132,6 +140,7 @@
132140
expect(test_entity.datetime).to eq(datetime)
133141
end
134142
end
143+
135144
context 'from a string' do
136145
context 'that can be parsed' do
137146
let(:datetime) { DateTime.now }
@@ -140,6 +149,7 @@
140149
expect(test_entity.datetime).to eq(datetime)
141150
end
142151
end
152+
143153
context 'that can NOT be parsed' do
144154
it 'should raise an error' do
145155
expect{ test_entity.datetime = 'ABC' }.to raise_error(ClassKit::Exceptions::InvalidAttributeValueError)
@@ -158,20 +168,23 @@
158168
expect(test_entity.time).to eq(time)
159169
end
160170
end
171+
161172
context 'from an integer' do
162173
let(:time) { Time.now }
163174
it 'should set the value' do
164175
expect{ test_entity.time = time.to_i }.not_to raise_error
165176
expect(test_entity.time.to_i).to eq(time.to_i)
166177
end
167178
end
179+
168180
context 'from a float' do
169181
let(:time) { Time.now }
170182
it 'should set the value' do
171183
expect{ test_entity.time = time.to_f }.not_to raise_error
172184
expect(test_entity.time.to_f).to eq(time.to_f)
173185
end
174186
end
187+
175188
context 'from a string' do
176189
context 'that can be parsed' do
177190
let(:time) { Time.now }
@@ -180,6 +193,7 @@
180193
expect(test_entity.time).to eq(time)
181194
end
182195
end
196+
183197
context 'that can NOT be parsed' do
184198
it 'should raise an error' do
185199
expect{ test_entity.time = 'ABC' }.to raise_error(ClassKit::Exceptions::InvalidAttributeValueError)
@@ -207,22 +221,26 @@
207221
expect{ test_entity.bool = true }.not_to raise_error
208222
expect(test_entity.bool).to eq(true)
209223
end
224+
210225
it 'should set the value to false' do
211226
expect{ test_entity.bool = false }.not_to raise_error
212227
expect(test_entity.bool).to eq(false)
213228
end
214229
end
230+
215231
context 'from a string' do
216232
context 'that can be parsed' do
217233
it 'should parse and set the value to true' do
218234
expect{ test_entity.bool = 'true' }.not_to raise_error
219235
expect(test_entity.bool).to eq(true)
220236
end
237+
221238
it 'should parse and set the value to false' do
222239
expect{ test_entity.bool = 'false' }.not_to raise_error
223240
expect(test_entity.bool).to eq(false)
224241
end
225242
end
243+
226244
context 'that can NOT be parsed' do
227245
it 'should raise an error' do
228246
expect{ test_entity.bool = 'ABC' }.to raise_error(ClassKit::Exceptions::InvalidAttributeValueError)
@@ -240,12 +258,14 @@
240258
end
241259
end
242260
end
261+
243262
context 'when allowed to be nil' do
244263
it 'should not raise an error when setting to nil' do
245264
expect{ test_entity.any_nil = nil }.not_to raise_error
246265
expect(test_entity.any_nil).to be nil
247266
end
248267
end
268+
249269
it 'should allow any value type to be specified' do
250270
expect{ test_entity.any = 'hello' }.not_to raise_error
251271
expect{ test_entity.any = 5 }.not_to raise_error
@@ -260,6 +280,7 @@
260280
expect{ test_entity.hash = { key1: 'value1' } }.not_to raise_error
261281
end
262282
end
283+
263284
context 'from a String' do
264285
it 'should raise an error' do
265286
expect{ test_entity.hash = 'ABC' }.to raise_error(ClassKit::Exceptions::InvalidAttributeValueError)
@@ -275,6 +296,7 @@
275296
expect{ test_entity.array = ['value1', 'value2'] }.not_to raise_error
276297
end
277298
end
299+
278300
context 'from a String' do
279301
it 'should raise an error' do
280302
expect{ test_entity.array = 'ABC' }.to raise_error(ClassKit::Exceptions::InvalidAttributeValueError)
@@ -283,19 +305,44 @@
283305
end
284306
end
285307

308+
context 'one_of attribute specified' do
309+
context 'when setting the attribute value' do
310+
context 'from an Array' do
311+
it 'should set the value' do
312+
expect{ test_entity.one_of = ['Happy Days'] }.not_to raise_error
313+
end
314+
end
315+
316+
context 'from a Hash' do
317+
it 'should set the value' do
318+
expect{ test_entity.one_of = { 'some' => 'structured data' } }.not_to raise_error
319+
end
320+
end
321+
322+
context 'from boolean' do
323+
it 'should parse and set the value to false' do
324+
expect{ test_entity.one_of = 'false' }.not_to raise_error
325+
expect(test_entity.one_of).to eq(false)
326+
end
327+
end
328+
end
329+
end
330+
286331
context 'Class Type attribute specified' do
287332
context 'when setting the attribute value' do
288333
context 'from a TestAddress' do
289334
it 'should set the value' do
290335
expect{ test_entity.address = TestAddress.new }.not_to raise_error
291336
end
292337
end
338+
293339
context 'from a String' do
294340
it 'should raise an error' do
295341
expect{ test_entity.address = 'ABC' }.to raise_error(ClassKit::Exceptions::InvalidAttributeValueError)
296342
end
297343
end
298344
end
345+
299346
context 'when auto_init is true' do
300347
context 'and the attribute has not been set' do
301348
it 'should return a new instance of the attribute type' do
@@ -305,8 +352,5 @@
305352
end
306353
end
307354
end
308-
309355
end
310356
end
311-
312-

spec/class_kit/test_objects.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class TestEntity
6060
attr_accessor_type :bool, type: :bool
6161
attr_accessor_type :hash, type: Hash
6262
attr_accessor_type :array, type: Array
63+
attr_accessor_type :one_of, one_of: [Hash, Array, :bool]
6364

6465
attr_accessor_type :address, type: TestAddress, allow_nil: false
6566
attr_accessor_type :address_auto, type: TestAddress, allow_nil: false, auto_init: true
@@ -71,20 +72,24 @@ class TestEntity
7172
class InvalidClass
7273
attr_accessor :text
7374
end
75+
7476
class TestParent
7577
extend ClassKit
7678
attr_accessor_type :base1, type: String
7779
attr_accessor_type :base2
7880
end
81+
7982
class TestChild < TestParent
8083
extend ClassKit
8184
attr_accessor_type :child1, type: String
8285
attr_accessor_type :child2
8386
end
87+
8488
class TestChild2 < TestParent
8589
extend ClassKit
8690
attr_accessor_type :text, type: String
8791
end
92+
8893
class TestChild3 < TestParent
8994
extend ClassKit
9095
attr_accessor_type :text1, type: String

0 commit comments

Comments
 (0)