attributor 5.6 → 5.7

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: 95e7a4d30473d5e7e9dbd37f0e028b9b5dd5b17cf0ceb89593f0aed60dab20de
4
- data.tar.gz: 59cc3458cd723c43733ea6a128fb4032c8aa8c7cd2ac41ea620c31842da35db6
3
+ metadata.gz: 72e523c77927ca8972951fa08ca3d9171f7601053d2a6d2ead46da6a9a1e2c4b
4
+ data.tar.gz: d6e008f822dfd90cb035dfdf66fd52d9fbea8bba35f02073dd2363aa4714ec64
5
5
  SHA512:
6
- metadata.gz: 10da45417633b8f159db72d33a81101f25bd90cf8282e3a10e80d3cfb92de90639642dcf51d12d8f8ce4237acd1e2e4e562c97173480b1525cb4914f237d8ce4
7
- data.tar.gz: e5c2ebc2dd2a214ffb4254332e56366bd825f1dbc93499d26476ed73226dc0e698be75918dab0a5d9853550ba8c88c83ba02afeeae22885254f828d2ac52040e
6
+ metadata.gz: 3404afef9afcc264b5394357c292155faa8ccf231e2f266b8e986d4234a16336a9d85b9e457d2bd16515bd01c95523460f1842dcdbde721d888dd3d80560c2b2
7
+ data.tar.gz: 65bc2b01956e42002e0b4a2e6fa54e93edd033dac3dae883225595f186b4f460823ffc82d32d5a6c9eba1a2ee7116325ca3d4af31e27f536462e6fd14fdc68fb
@@ -1,6 +1,8 @@
1
1
  # Attributor Changelog
2
2
 
3
- ## next
3
+ ## 5.7 (1/7/2021)
4
+
5
+ - added `custom_option` to Attributor::Attribute class, accepting a name and Attribute arguments that will be used to validate the option value(s) provided.
4
6
 
5
7
  ## 5.6 (11/02/2020)
6
8
 
@@ -17,11 +17,25 @@ module Attributor
17
17
  FakeParent
18
18
  end
19
19
  end
20
+
20
21
  # It is the abstract base class to hold an attribute, both a leaf and a container (hash/Array...)
21
22
  # TODO: should this be a mixin since it is an abstract class?
22
23
  class Attribute
23
24
  attr_reader :type, :options
24
25
 
26
+ @custom_options = {}
27
+
28
+ class << self
29
+ attr_accessor :custom_options
30
+ end
31
+
32
+ def self.custom_option(name, attr_type, options = {}, &block)
33
+ if TOP_LEVEL_OPTIONS.include?(name) || INTERNAL_OPTIONS.include?(name)
34
+ raise ArgumentError, "can not define custom_option with name #{name.inspect}, it is reserved by Attributor"
35
+ end
36
+ self.custom_options[name] = Attributor::Attribute.new(attr_type, options, &block)
37
+ end
38
+
25
39
  # @options: metadata about the attribute
26
40
  # @block: code definition for struct attributes (nil for predefined types or leaf/simple types)
27
41
  def initialize(type, options = {}, &block)
@@ -175,6 +189,11 @@ module Attributor
175
189
  # TODO: not sure if that's correct (we used to get it from the described hash...
176
190
  description[:example] = self.dump(example) if example
177
191
 
192
+ # add custom options as x-optionname
193
+ self.class.custom_options.each do |name, _|
194
+ description["x-#{name}".to_sym] = self.options[name] if self.options.key?(name)
195
+ end
196
+
178
197
  description
179
198
  end
180
199
 
@@ -296,6 +315,8 @@ module Attributor
296
315
 
297
316
  # TODO: override in type subclass
298
317
  def check_option!(name, definition)
318
+ return check_custom_option(name, definition) if self.class.custom_options.include? name
319
+
299
320
  case name
300
321
  when :values
301
322
  raise AttributorException, "Allowed set of values requires an array. Got (#{definition})" unless definition.is_a? ::Array
@@ -322,5 +343,14 @@ module Attributor
322
343
 
323
344
  :ok # passes
324
345
  end
346
+
347
+ def check_custom_option(name, definition)
348
+ attribute = self.class.custom_options.fetch(name)
349
+
350
+ errors = attribute.validate(definition)
351
+ raise AttributorException, "Custom option #{name.inspect} is invalid: #{errors.inspect}" if errors.any?
352
+
353
+ :ok
354
+ end
325
355
  end
326
356
  end
@@ -1,3 +1,3 @@
1
1
  module Attributor
2
- VERSION = '5.6'.freeze
2
+ VERSION = '5.7'.freeze
3
3
  end
@@ -62,6 +62,7 @@ describe Attributor::Attribute do
62
62
  it 'as well as the type-specific ones' do
63
63
  expect(js[:type]).to eq(:integer)
64
64
  end
65
+
65
66
  end
66
67
 
67
68
  end
@@ -194,6 +195,49 @@ describe Attributor::Attribute do
194
195
  end.not_to raise_error
195
196
  end
196
197
  end
198
+
199
+ context 'custom_options' do
200
+ let(:option_name) { :foo }
201
+ let(:custom_option_args) { [option_name, String] }
202
+
203
+ around do |example|
204
+ Attributor::Attribute.custom_option *custom_option_args
205
+ example.run
206
+ Attributor::Attribute.custom_options.delete option_name
207
+ end
208
+
209
+ it 'raises ArgumentError if given an existing option' do
210
+ expect {
211
+ Attributor::Attribute.custom_option :default, Object
212
+ }.to raise_error(ArgumentError)
213
+ end
214
+
215
+ it 'accepts custom options' do
216
+ expect do
217
+ Attributor::Attribute.new(Integer, foo: 'unvalidated')
218
+ end.not_to raise_error
219
+ end
220
+
221
+ context 'can validate the custom option value' do
222
+ let(:custom_option_args) { [option_name, String, values: ['valid']] }
223
+ it 'does not raise with a valid option value' do
224
+ expect do
225
+ Attributor::Attribute.new(Integer, foo: 'valid')
226
+ end.not_to raise_error
227
+ end
228
+ it 'raises with an invalid option value' do
229
+ expect do
230
+ Attributor::Attribute.new(Integer, foo: 'invalid')
231
+ end.to raise_error(Attributor::AttributorException)
232
+ end
233
+ end
234
+
235
+ it 'appear in as_json_schema' do
236
+ attribute = Attributor::Attribute.new(Integer, foo: 'valid')
237
+ json_schema = attribute.as_json_schema
238
+ expect(json_schema[:'x-foo']).to eq 'valid'
239
+ end
240
+ end
197
241
  end
198
242
 
199
243
  context 'example' do
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attributor
3
3
  version: !ruby/object:Gem::Version
4
- version: '5.6'
4
+ version: '5.7'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josep M. Blanquer
8
8
  - Dane Jensen
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-11-02 00:00:00.000000000 Z
12
+ date: 2021-01-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: hashie
@@ -291,7 +291,7 @@ dependencies:
291
291
  - - ">="
292
292
  - !ruby/object:Gem::Version
293
293
  version: '0'
294
- description:
294
+ description:
295
295
  email:
296
296
  - blanquer@gmail.com
297
297
  - dane.jensen@gmail.com
@@ -392,7 +392,7 @@ homepage: https://github.com/rightscale/attributor
392
392
  licenses:
393
393
  - MIT
394
394
  metadata: {}
395
- post_install_message:
395
+ post_install_message:
396
396
  rdoc_options: []
397
397
  require_paths:
398
398
  - lib
@@ -408,7 +408,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
408
408
  version: '0'
409
409
  requirements: []
410
410
  rubygems_version: 3.0.3
411
- signing_key:
411
+ signing_key:
412
412
  specification_version: 4
413
413
  summary: A powerful attribute and type management library for Ruby
414
414
  test_files: