requisite 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fefe3a8b4f675162d0bf7ac4b8302d84845fd9437e1e358493acf1960d677207
4
- data.tar.gz: 307e2a0d91ba1be12292e58da0dd89ad5deeed52b469e7c7c0b28b4ec4ff2b55
3
+ metadata.gz: 22ae33af68416be1beb8abf9c3384934e4c93f1a46ab089c05436956a7391e3a
4
+ data.tar.gz: ef8a85a1fb0af0aedee42f635283590306016e0101adef510365f065ae6cb908
5
5
  SHA512:
6
- metadata.gz: 341d9dcc25d67f708964dce7807b2b69e90fc11426f1190d4664506d2bf08451afabeace89adef613efba625f3c3fb6f842cc8cf3e5aa554d422db5cc8696032
7
- data.tar.gz: '03853411c5332125af440424210f55fe067d23ba4308aca4de8a8bc1a2f2d899fa246542543b00f75ef6e61d80db780f15d638d21c25081a0f01fa853549fe14'
6
+ metadata.gz: 35f9ebfbf4ff91f05067c422e27b13caed813d926093f0c5bf792375be57cdb03a5451e8690a999417826334d762484e63eb58025c0eafda6bade2db210d5615
7
+ data.tar.gz: 362d50d708a1d1f80002dc751fa41cf5baf8ed1dea3cd66725e80472df2fd1dcb9d9224964699cd06a3deecfccd8e7c10466dc852a89731b8b55169f65c0fceb
data/.gitignore CHANGED
@@ -1 +1,2 @@
1
- *.gem
1
+ *.gem
2
+ Gemfile.lock
@@ -9,7 +9,7 @@ module Requisite
9
9
  result = self.send(:parse_typed_hash, resolved_name, options[:typed_hash]) if options[:typed_hash]
10
10
  result = self.send(:parse_scalar_hash, resolved_name) if options[:scalar_hash]
11
11
  result = self.send(:parse_typed_array, resolved_name, options[:typed_array]) if options[:typed_array]
12
- result = options[:default] if (options[:default] && empty_result?(result))
12
+ result = options[:default] if (options.key?(:default) && empty_result?(result))
13
13
  raise_bad_type_if_type_mismatch(result, options[:type]) if options[:type] && result
14
14
  result = result.to_s if options[:stringify]
15
15
  result
@@ -50,7 +50,7 @@ module Requisite
50
50
  self.singleton_class.send(:alias_method, :a!, :attribute!)
51
51
 
52
52
  def raise_bad_type_if_type_mismatch(value, desired_type)
53
- raise BadTypeError.new(value, desired_type) unless (value.kind_of?(desired_type)) || ((value.kind_of?(TrueClass) || value.kind_of?(TrueClass)) && desired_type == Requisite::Boolean)
53
+ raise BadTypeError.new(value, desired_type) unless (value.kind_of?(desired_type)) || ((value.kind_of?(TrueClass) || value.kind_of?(FalseClass)) && desired_type == Requisite::Boolean)
54
54
  end
55
55
 
56
56
  def raise_not_implemented_for_attribute(name)
@@ -1,3 +1,3 @@
1
1
  module Requisite
2
- VERSION = '0.4.1'
2
+ VERSION = '0.4.2'
3
3
  end
@@ -1,5 +1,4 @@
1
1
  require 'test_helper'
2
- # require_relative '../dummy_api_model'
3
2
 
4
3
  module Requisite
5
4
  class DummyApiModel < Requisite::ApiModel
@@ -11,16 +10,16 @@ module Requisite
11
10
  response = DummyApiModel.new
12
11
  def response.a; 'A'; end
13
12
  def response.b; 2; end
14
- response.to_hash.must_equal( :a => 'A', :b => 2 )
15
- response.must_respond_to :a
16
- response.must_respond_to :b
13
+ _(response.to_hash).must_equal( :a => 'A', :b => 2 )
14
+ _(response).must_respond_to :a
15
+ _(response).must_respond_to :b
17
16
  end
18
17
 
19
18
  it 'attribute provides a default implementation of calling a hash model' do
20
19
  DummyApiModel.serialized_attributes { attribute :c }
21
20
  mock = {:c => 'C'}
22
21
  response = DummyApiModel.new(mock)
23
- response.to_hash.must_equal( :c => 'C' )
22
+ _(response.to_hash).must_equal( :c => 'C' )
24
23
  end
25
24
 
26
25
  let(:params_hash) { {:c => 'C', :num => 12} }
@@ -28,89 +27,101 @@ module Requisite
28
27
  it 'attribute provides a default implementation of calling a model' do
29
28
  DummyApiModel.serialized_attributes { attribute :c }
30
29
  response = DummyApiModel.new(params_hash)
31
- response.to_hash.must_equal(:c => 'C')
30
+ _(response.to_hash).must_equal(:c => 'C')
32
31
  end
33
32
 
34
33
  it 'attribute can work with a default' do
35
34
  DummyApiModel.serialized_attributes { attribute :c, default: 'see' }
36
35
  response = DummyApiModel.new
37
- response.to_hash.must_equal(:c => 'see')
36
+ _(response.to_hash).must_equal(:c => 'see')
38
37
  end
39
38
 
40
39
  it 'ignores default if value given' do
41
40
  DummyApiModel.serialized_attributes { attribute :num, default: 0 }
42
41
  response = DummyApiModel.new(params_hash)
43
- response.to_hash.must_equal(:num => 12)
42
+ _(response.to_hash).must_equal(:num => 12)
43
+ end
44
+
45
+ it 'can set a default value of true for a boolean attribute' do
46
+ DummyApiModel.serialized_attributes { attribute :truthy_val, type: Requisite::Boolean, default: true }
47
+ response = DummyApiModel.new
48
+ _(response.to_hash).must_equal(:truthy_val => true)
49
+ end
50
+
51
+ it 'can set a default value of false for a boolean attribute' do
52
+ DummyApiModel.serialized_attributes { attribute :truthy_val, type: Requisite::Boolean, default: false }
53
+ response = DummyApiModel.new
54
+ _(response.to_hash).must_equal(:truthy_val => false)
44
55
  end
45
56
 
46
57
  it 'attribute can be set to stringify fields' do
47
58
  DummyApiModel.serialized_attributes { attribute :num, stringify: true }
48
59
  response = DummyApiModel.new(params_hash)
49
- response.to_hash.must_equal(:num => '12')
60
+ _(response.to_hash).must_equal(:num => '12')
50
61
  end
51
62
 
52
63
  it 'attribute can be set to rename fields' do
53
64
  DummyApiModel.serialized_attributes { attribute :my_num, rename: :num }
54
65
  response = DummyApiModel.new(params_hash)
55
- response.to_hash.must_equal(:my_num => 12)
66
+ _(response.to_hash).must_equal(:my_num => 12)
56
67
  end
57
68
 
58
69
  it 'attribute can assert type of a field' do
59
70
  DummyApiModel.serialized_attributes { attribute :num, type: String }
60
71
  response = DummyApiModel.new(params_hash)
61
- proc { response.to_hash }.must_raise(BadTypeError)
72
+ _(proc { response.to_hash }).must_raise(BadTypeError)
62
73
  end
63
74
 
64
75
  it 'with_type! helper raises on mismatched type' do
65
76
  model = DummyApiModel.new()
66
- proc { model.send(:with_type!, String) { 1 + 2 }}.must_raise(Requisite::BadTypeError)
77
+ _(proc { model.send(:with_type!, String) { 1 + 2 }}).must_raise(Requisite::BadTypeError)
67
78
  end
68
79
 
69
80
  it 'first_attribute_from_model helper finds first matching attriubute' do
70
81
  model = DummyApiModel.new(:oh => 12, :a => nil, :b => 'B', :c => 'C')
71
- model.send(:first_attribute_from_model, :a, :b, :c).must_equal('B')
82
+ _(model.send(:first_attribute_from_model, :a, :b, :c)).must_equal('B')
72
83
  end
73
84
 
74
85
  it 'attribute can assert type of a boolean field' do
75
86
  DummyApiModel.serialized_attributes { attribute :truthy_val, type: Requisite::Boolean }
76
87
  response = DummyApiModel.new(:truthy_val => false)
77
- response.to_hash.must_equal(:truthy_val => false)
88
+ _(response.to_hash).must_equal(:truthy_val => false)
78
89
  end
79
90
 
80
91
  it 'attribute does not include values of nil' do
81
92
  DummyApiModel.serialized_attributes { attribute :num, type: String }
82
93
  response = DummyApiModel.new({:num => nil})
83
- response.to_hash.must_equal({})
94
+ _(response.to_hash).must_equal({})
84
95
  end
85
96
 
86
97
  it 'attribute includes values of nil if permitted' do
87
98
  DummyApiModel.serialized_attributes { attribute :num, type: String }
88
99
  response = DummyApiModel.new({:num => nil})
89
- response.to_hash(show_nil: true).must_equal({:num => nil})
100
+ _(response.to_hash(show_nil: true)).must_equal({:num => nil})
90
101
  end
91
102
 
92
103
  it 'attribute can be stringified and renamed with default fields' do
93
104
  DummyApiModel.serialized_attributes { attribute :my_num, rename: :num, stringify: true, default: 22 }
94
105
  response = DummyApiModel.new
95
- response.to_hash.must_equal(:my_num => '22')
106
+ _(response.to_hash).must_equal(:my_num => '22')
96
107
  end
97
108
 
98
109
  it 'attribute can be stringified after type check' do
99
110
  DummyApiModel.serialized_attributes { attribute :num, stringify: true, type: Fixnum }
100
111
  response = DummyApiModel.new(params_hash)
101
- response.to_hash.must_equal(:num => '12')
112
+ _(response.to_hash).must_equal(:num => '12')
102
113
  end
103
114
 
104
115
  it 'attribute type checks after rename' do
105
116
  DummyApiModel.serialized_attributes { attribute :my_num, rename: :num, type: String }
106
117
  response = DummyApiModel.new(params_hash)
107
- proc { response.to_hash }.must_raise(BadTypeError)
118
+ _(proc { response.to_hash }).must_raise(BadTypeError)
108
119
  end
109
120
 
110
121
  it 'attribute can be stringified, renamed, defaulted and have type checking on a field' do
111
122
  DummyApiModel.serialized_attributes { attribute :my_num, rename: :num, stringify: true, default: 22, type: String }
112
123
  response = DummyApiModel.new
113
- proc { response.to_hash }.must_raise(BadTypeError)
124
+ _(proc { response.to_hash }).must_raise(BadTypeError)
114
125
  end
115
126
 
116
127
  let(:invalid_params_hash) { {:d => nil} }
@@ -118,32 +129,32 @@ module Requisite
118
129
  it "attribute! raises an error if not found on model" do
119
130
  DummyApiModel.serialized_attributes { attribute! :d }
120
131
  response = DummyApiModel.new(invalid_params_hash)
121
- proc { response.to_hash }.must_raise(NotImplementedError, "'d' not found on model")
132
+ _(proc { response.to_hash }).must_raise(NotImplementedError, "'d' not found on model")
122
133
  end
123
134
 
124
135
  it 'attribute! does not raise an error if value on model is false' do
125
136
  params_hash = {:d => false}
126
137
  DummyApiModel.serialized_attributes { attribute! :d }
127
138
  response = DummyApiModel.new(params_hash)
128
- response.to_hash.must_equal({d: false})
139
+ _(response.to_hash).must_equal({d: false})
129
140
  end
130
141
 
131
142
  it 'attribute! can be set to stringify fields' do
132
143
  DummyApiModel.serialized_attributes { attribute! :num, stringify: true }
133
144
  response = DummyApiModel.new(params_hash)
134
- response.to_hash.must_equal(:num => '12')
145
+ _(response.to_hash).must_equal(:num => '12')
135
146
  end
136
147
 
137
148
  it 'attribute! can be set to rename fields' do
138
149
  DummyApiModel.serialized_attributes { attribute! :my_num, rename: :num }
139
150
  response = DummyApiModel.new(params_hash)
140
- response.to_hash.must_equal(:my_num => 12)
151
+ _(response.to_hash).must_equal(:my_num => 12)
141
152
  end
142
153
 
143
154
  it 'sets the model from a hash' do
144
155
  DummyApiModel.serialized_attributes { }
145
156
  response = DummyApiModel.new(params_hash)
146
- response.model.must_equal(params_hash)
157
+ _(response.model).must_equal(params_hash)
147
158
  end
148
159
 
149
160
  it 'sets the model from an object' do
@@ -152,39 +163,39 @@ module Requisite
152
163
  mc.b = 2
153
164
  DummyApiModel.serialized_attributes { attribute :a }
154
165
  response = DummyApiModel.new(mc)
155
- response.model.must_equal(mc)
156
- response.to_hash.must_equal(:a => 'a')
166
+ _(response.model).must_equal(mc)
167
+ _(response.to_hash).must_equal(:a => 'a')
157
168
  end
158
169
 
159
170
  it 'has alias a for attribute' do
160
171
  DummyApiModel.serialized_attributes { a :num }
161
172
  response = DummyApiModel.new(params_hash)
162
- response.to_hash.must_equal(:num => 12)
173
+ _(response.to_hash).must_equal(:num => 12)
163
174
  end
164
175
 
165
176
  it 'has alias a! for attribute!' do
166
177
  DummyApiModel.serialized_attributes { a! :num }
167
178
  response = DummyApiModel.new(params_hash)
168
- response.to_hash.must_equal(:num => 12)
179
+ _(response.to_hash).must_equal(:num => 12)
169
180
  end
170
181
 
171
182
  it 'can convert to json' do
172
183
  DummyApiModel.serialized_attributes { a! :num }
173
184
  response = DummyApiModel.new(params_hash)
174
- response.to_json.must_equal("{\"num\":12}")
185
+ _(response.to_json).must_equal("{\"num\":12}")
175
186
  end
176
187
 
177
188
  it 'drops non-listed parameters' do
178
189
  DummyApiModel.serialized_attributes { attribute :num }
179
190
  response = DummyApiModel.new({num: 12, other: 'value'})
180
- response.to_hash.must_equal(:num => 12)
191
+ _(response.to_hash).must_equal(:num => 12)
181
192
  end
182
193
 
183
194
  it 'works with ActionController::Parameters' do
184
195
  DummyApiModel.serialized_attributes { attribute :num }
185
196
  params = ActionController::Parameters.new(num: 12)
186
197
  response = DummyApiModel.new(params)
187
- response.to_hash.must_equal(:num => 12)
198
+ _(response.to_hash).must_equal(:num => 12)
188
199
  end
189
200
 
190
201
  describe 'with nested structures' do
@@ -193,13 +204,13 @@ module Requisite
193
204
  it 'allows arrays of one type' do
194
205
  DummyApiModel.serialized_attributes { attribute :ids, typed_array: Fixnum }
195
206
  response = DummyApiModel.new({ids: [1, 2, 3]})
196
- response.to_hash.must_equal(:ids => [1, 2, 3])
207
+ _(response.to_hash).must_equal(:ids => [1, 2, 3])
197
208
  end
198
209
 
199
210
  it 'raises errors when array has a wrongly typed value' do
200
211
  DummyApiModel.serialized_attributes { attribute :ids, typed_array: Requisite::Boolean }
201
212
  response = DummyApiModel.new({ids: [true, 'value', false]})
202
- Proc.new {response.to_hash}.must_raise(BadTypeError)
213
+ _(Proc.new {response.to_hash}).must_raise(BadTypeError)
203
214
  end
204
215
  end
205
216
 
@@ -207,42 +218,42 @@ module Requisite
207
218
  it 'drops non listed parameters in nested hashes' do
208
219
  DummyApiModel.serialized_attributes { attribute :data, typed_hash: { num: Numeric, bool: Requisite::Boolean } }
209
220
  response = DummyApiModel.new({data: { num: 12, value: 'x', bool: true }})
210
- response.to_hash.must_equal(:data => { :num => 12, :bool => true })
221
+ _(response.to_hash).must_equal(:data => { :num => 12, :bool => true })
211
222
  end
212
223
 
213
224
  it 'can stringify nested hashes' do
214
225
  DummyApiModel.serialized_attributes { attribute :data, typed_hash: { num: Numeric }, stringify: true }
215
226
  response = DummyApiModel.new({data: { num: 12, value: 'x' }})
216
- response.to_hash.must_equal(:data => "{:num=>12}")
227
+ _(response.to_hash).must_equal(:data => "{:num=>12}")
217
228
  end
218
229
 
219
230
  it 'raises an error when nested hash values of the wrong type' do
220
231
  DummyApiModel.serialized_attributes { attribute :data, typed_hash: { num: Numeric } }
221
- Proc.new {DummyApiModel.new({data: { num: '12'}}).to_hash}.must_raise(BadTypeError)
232
+ _(Proc.new {DummyApiModel.new({data: { num: '12'}}).to_hash}).must_raise(BadTypeError)
222
233
  end
223
234
 
224
235
  it 'can rename param and work with nested hashes' do
225
236
  DummyApiModel.serialized_attributes { attribute :my_data, typed_hash: { num: Numeric }, rename: :data }
226
237
  response = DummyApiModel.new({data: { num: 12, value: 'x' }})
227
- response.to_hash.must_equal(:my_data => { :num => 12 })
238
+ _(response.to_hash).must_equal(:my_data => { :num => 12 })
228
239
  end
229
240
 
230
241
  it 'can set a default value for a nested hash' do
231
242
  DummyApiModel.serialized_attributes { attribute :data, typed_hash: { num: Numeric }, default: { num: 4 } }
232
243
  response = DummyApiModel.new({data: { value: 'x' }})
233
- response.to_hash.must_equal(:data => { :num => 4 })
244
+ _(response.to_hash).must_equal(:data => { :num => 4 })
234
245
  end
235
246
 
236
247
  it 'drops non listed fields with attribute!' do
237
248
  DummyApiModel.serialized_attributes { attribute! :data, typed_hash: { num: Numeric } }
238
249
  response = DummyApiModel.new({data: { num: 12, value: 'x' }})
239
- response.to_hash.must_equal(:data => { :num => 12 })
250
+ _(response.to_hash).must_equal(:data => { :num => 12 })
240
251
  end
241
252
 
242
253
  it 'attribute! does not raise an error with missing values in hash' do
243
254
  DummyApiModel.serialized_attributes { attribute! :data, typed_hash: { num: Numeric } }
244
255
  response = DummyApiModel.new({data: { value: 'x' }})
245
- response.to_hash.must_equal(:data => { })
256
+ _(response.to_hash).must_equal(:data => { })
246
257
  end
247
258
  end
248
259
 
@@ -250,19 +261,19 @@ module Requisite
250
261
  it 'should parse scalar hashes permitting anything scalar' do
251
262
  DummyApiModel.serialized_attributes { attribute :data, scalar_hash: true }
252
263
  response = DummyApiModel.new({data: { num: 12, value: 'x', :truthy => false }})
253
- response.to_hash.must_equal(:data => { :num => 12, :value => 'x', :truthy => false })
264
+ _(response.to_hash).must_equal(:data => { :num => 12, :value => 'x', :truthy => false })
254
265
  end
255
266
 
256
267
  it 'should parse a renamed scalar hash' do
257
268
  DummyApiModel.serialized_attributes { attribute :my_data, scalar_hash: true, rename: :data }
258
269
  response = DummyApiModel.new({data: { num: 12, value: 'x' }})
259
- response.to_hash.must_equal(:my_data => { :num => 12, :value => 'x' })
270
+ _(response.to_hash).must_equal(:my_data => { :num => 12, :value => 'x' })
260
271
  end
261
272
 
262
273
  it 'should stringify a scalar hash' do
263
274
  DummyApiModel.serialized_attributes { attribute :data, scalar_hash: true, stringify: true }
264
275
  response = DummyApiModel.new({data: { num: 12, value: 'x' }})
265
- response.to_hash.must_equal(:data => "{:num=>12, :value=>\"x\"}")
276
+ _(response.to_hash).must_equal(:data => "{:num=>12, :value=>\"x\"}")
266
277
  end
267
278
 
268
279
  it 'should parse scalar hashes permitting anything scalar with object' do
@@ -271,13 +282,13 @@ module Requisite
271
282
  mc.b = { num: 12, value: 'x' }
272
283
  DummyApiModel.serialized_attributes { attribute :b, scalar_hash: true }
273
284
  response = DummyApiModel.new(mc)
274
- response.to_hash.must_equal(:b => { :num => 12, :value => 'x' })
285
+ _(response.to_hash).must_equal(:b => { :num => 12, :value => 'x' })
275
286
  end
276
287
 
277
288
  it 'should fail to parse scalar hashes when non scalar values present' do
278
289
  DummyApiModel.serialized_attributes { attribute :data, scalar_hash: true }
279
- Proc.new { DummyApiModel.new({data: { num: 12, value: { nested: 'value' } }}).to_hash}.must_raise(BadTypeError)
280
- Proc.new { DummyApiModel.new({data: { num: 12, value: ['array value'] }}).to_hash}.must_raise(BadTypeError)
290
+ _(Proc.new { DummyApiModel.new({data: { num: 12, value: { nested: 'value' } }}).to_hash}).must_raise(BadTypeError)
291
+ _(Proc.new { DummyApiModel.new({data: { num: 12, value: ['array value'] }}).to_hash}).must_raise(BadTypeError)
281
292
  end
282
293
 
283
294
  it 'should fail to parse scalar hashes permitting anything scalar with object' do
@@ -286,25 +297,25 @@ module Requisite
286
297
  mc.b = { value: { nested: 'value' } }
287
298
  DummyApiModel.serialized_attributes { attribute :b, scalar_hash: true }
288
299
  response = DummyApiModel.new(mc)
289
- Proc.new { response.to_hash }.must_raise(BadTypeError)
300
+ _(Proc.new { response.to_hash }).must_raise(BadTypeError)
290
301
  end
291
302
 
292
303
  it 'can set a default value for a scalar hash' do
293
304
  DummyApiModel.serialized_attributes { attribute :data, scalar_hash: true, default: { num: 9, value: 'y' } }
294
305
  response = DummyApiModel.new({data: { }})
295
- response.to_hash.must_equal(:data => { :num => 9, :value => 'y' })
306
+ _(response.to_hash).must_equal(:data => { :num => 9, :value => 'y' })
296
307
  end
297
308
 
298
309
  it 'doesnt raise with attribute! when an empty hash passed' do
299
310
  DummyApiModel.serialized_attributes { attribute! :data, scalar_hash: true }
300
311
  response = DummyApiModel.new({data: {}})
301
- response.to_hash.must_equal(:data => {})
312
+ _(response.to_hash).must_equal(:data => {})
302
313
  end
303
314
 
304
315
  it 'raises with attribute! when nil is passed' do
305
316
  DummyApiModel.serialized_attributes { attribute! :data, scalar_hash: true }
306
317
  response = DummyApiModel.new({data: nil})
307
- Proc.new {response.to_hash}.must_raise(NotImplementedError)
318
+ _(Proc.new {response.to_hash}).must_raise(NotImplementedError)
308
319
  end
309
320
  end
310
321
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: requisite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Osler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-02-06 00:00:00.000000000 Z
11
+ date: 2022-09-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -98,7 +98,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
98
98
  version: '0'
99
99
  requirements: []
100
100
  rubyforge_project:
101
- rubygems_version: 2.7.6
101
+ rubygems_version: 2.7.6.2
102
102
  signing_key:
103
103
  specification_version: 4
104
104
  summary: Strongly defined models for HTTP APIs