attributor 7.0 → 7.1

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: 5b5fc3c6809e4a0e21b21752683196ce018beafbb7d8b615f2217abbcb55b3c3
4
- data.tar.gz: 55bba33e0b5ee71c581a029cfd3045c6f71ce5d1f578b2654d99f919626dcbc8
3
+ metadata.gz: 6207241fe519ee3ee26e4a4220077428c26850c556abd065d7c073125c0a49fa
4
+ data.tar.gz: bb19a849faf087d1f9fad40c9b3101b4a416ef32ef8f4d266b6a061c37f270e2
5
5
  SHA512:
6
- metadata.gz: f09888249f30f079afcbfbc09695cf828bed15d6c6b621b0ab5a55de086b740c74bfa33cfd4b75fa1c97650ccb0c7ceeb5850401af94a11619ea46b6c0aada33
7
- data.tar.gz: b3876fbc0f432ff67fc0031234a4da8eea6b2b965d539a71f5a27ab615442cc8ae2420270a315a2d1461fb20d3f9aa7eae8619a1e7979f9f6cb73365d786cddd
6
+ metadata.gz: 5bbec43bae528b47ddc5176e75bbd07f84707520a363e08257dff88409d1a7b1f9febce1a77e2a99e4df07537d0ed6253d4e6911e453a51f28bcb3f7d445b6ff
7
+ data.tar.gz: 12726d8e722d4ddc28c04bb6d03d3fae81b5f4abe84268f1e335f80a6e84880dcddc96ca12a9d1281abf4fac31c900d644c2f3e1ef9e1ff0d1929aeae0afe325
data/CHANGELOG.md CHANGED
@@ -2,6 +2,11 @@
2
2
 
3
3
  ## next
4
4
 
5
+ ## 7.1 (6/9/2023)
6
+ - Enhance custom_option handling. The values provided for custom options are now loaded (and definition will fail if they aren't compatible). This was specially
7
+ important for custom options that were collections, etc.
8
+ - Fix Float type .load
9
+
5
10
  ## 7.0 (5/23/2023)
6
11
  - Support for loading "<digits>." strings in BigDecimal/Float types (These are formats supported by JS, Java ..)
7
12
  - Support for defining collections of types using a more terse DSL: T[]
@@ -42,6 +42,17 @@ module Attributor
42
42
  @type = Attributor.resolve_type(type, options, block)
43
43
  @options = @type.respond_to?(:options) ? @type.options.merge(options) : options
44
44
 
45
+ # We will give the values passed for options, a chance to be 'loaded' by its type, so we store native loaded values in the options
46
+ begin
47
+ current_option_name = nil # Use this to avoid having to wrap each loop with a begin/rescue block
48
+ (self.class.custom_options.keys & @options.keys).each do |custom_key|
49
+ current_option_name = custom_key
50
+ @options[custom_key] = self.class.custom_options[custom_key].load(@options[custom_key])
51
+ end
52
+ rescue => e
53
+ raise AttributorException, "Error while loading value #{@options[current_option_name]} for custom option '#{current_option_name}': #{e.message}"
54
+ end
55
+
45
56
  check_options!
46
57
  end
47
58
 
@@ -18,7 +18,7 @@ module Attributor
18
18
  end
19
19
 
20
20
  def self.load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, **options)
21
- return BigDecimal(value + '0') if value.is_a?(::String) && value.end_with?('.')
21
+ return Float(value + '0') if value.is_a?(::String) && value.end_with?('.')
22
22
  Float(value)
23
23
  rescue TypeError
24
24
  super
@@ -1,3 +1,3 @@
1
1
  module Attributor
2
- VERSION = '7.0'.freeze
2
+ VERSION = '7.1'.freeze
3
3
  end
@@ -232,6 +232,16 @@ describe Attributor::Attribute do
232
232
  end
233
233
  end
234
234
 
235
+ context 'validates the type of values' do
236
+ let(:custom_option_args) { [option_name, String ] }
237
+
238
+ it 'raises with an invalid option value' do
239
+ expect do
240
+ Attributor::Attribute.new(Integer, foo: {a:1})
241
+ end.to raise_error(Attributor::AttributorException,/Error while loading value {:a=>1} for custom option 'foo': Type Attributor::String cannot load values of type Hash/)
242
+ end
243
+ end
244
+
235
245
  it 'appear in as_json_schema' do
236
246
  attribute = Attributor::Attribute.new(Integer, foo: 'valid')
237
247
  json_schema = attribute.as_json_schema
@@ -61,8 +61,10 @@ describe Attributor::Float do
61
61
  expect(type.load('0.')).to eq Float('0.0')
62
62
  end
63
63
  it 'decodes it if the String represents a negative Float and ends in .' do
64
- expect(type.load('-10.')).to eq Float('-10.0')
65
- expect(type.load('-0.')).to eq Float('-0.0')
64
+ expect(type.load('-10.')).to eq -10.0
65
+ expect(type.load('-10.').class).to eq Float
66
+ expect(type.load('-0.')).to eq -0.0
67
+ expect(type.load('-0.').class).to eq Float
66
68
  end
67
69
  end
68
70
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attributor
3
3
  version: !ruby/object:Gem::Version
4
- version: '7.0'
4
+ version: '7.1'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josep M. Blanquer
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2023-05-23 00:00:00.000000000 Z
12
+ date: 2023-06-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: hashie