attributor 5.3 → 5.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/attributor.gemspec +0 -1
- data/lib/attributor.rb +14 -0
- data/lib/attributor/attribute.rb +2 -1
- data/lib/attributor/types/class.rb +1 -1
- data/lib/attributor/types/hash.rb +11 -2
- data/lib/attributor/types/polymorphic.rb +3 -1
- data/lib/attributor/version.rb +1 -1
- data/spec/types/class_spec.rb +0 -1
- metadata +2 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bb9d8a77a4e71c5f44cc2eaa6559c7d40882c88ec675b0400170d961386d66c1
|
4
|
+
data.tar.gz: 0b4d4bec370cbca4fa15d61dd08b0bc9b2cdcb8f1f0d39d21287e7471f60075e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 78a4367ada6bebcb2a164da33b08e05bd0c20207d11df7631b700346bb3e4ec74df47bef3373178cbd619a2f0252609c59586a1fb46c0913830a57b547361b67
|
7
|
+
data.tar.gz: e2a0fa2ecb088ff61c184624fcadfcc992d1fd60c5694727e8ffd594be1188ee6d60e2b14d7cc1daab4a9e8fbd67b584612d3be6e7574fbda4cfca004a182e00
|
data/attributor.gemspec
CHANGED
@@ -27,7 +27,6 @@ Gem::Specification.new do |spec|
|
|
27
27
|
spec.add_development_dependency 'rspec-its'
|
28
28
|
spec.add_development_dependency 'rspec-collection_matchers'
|
29
29
|
spec.add_development_dependency('yard')
|
30
|
-
spec.add_development_dependency('backports', ['~> 3'])
|
31
30
|
spec.add_development_dependency('yardstick', ['~> 0'])
|
32
31
|
spec.add_development_dependency('bundler', ['>= 0'])
|
33
32
|
spec.add_development_dependency('rake-notes', ['~> 0'])
|
data/lib/attributor.rb
CHANGED
@@ -73,6 +73,20 @@ module Attributor
|
|
73
73
|
inspection
|
74
74
|
end
|
75
75
|
|
76
|
+
def self.recursive_to_h(val)
|
77
|
+
if val.is_a? Array
|
78
|
+
val.map { |v| recursive_to_h(v) }
|
79
|
+
elsif val.nil?
|
80
|
+
nil
|
81
|
+
elsif val.respond_to?(:to_h)
|
82
|
+
val.to_h.each_with_object({}) do |(name, inner_val), hash|
|
83
|
+
hash[name] = recursive_to_h(inner_val)
|
84
|
+
end
|
85
|
+
else
|
86
|
+
val
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
76
90
|
MODULE_PREFIX = 'Attributor::'.freeze
|
77
91
|
MODULE_PREFIX_REGEX = ::Regexp.new(MODULE_PREFIX)
|
78
92
|
|
data/lib/attributor/attribute.rb
CHANGED
@@ -158,7 +158,8 @@ module Attributor
|
|
158
158
|
|
159
159
|
if options.key? :example
|
160
160
|
loaded = example_from_options(parent, context)
|
161
|
-
|
161
|
+
# Only validate the type, if the proc-generated example is "complex" (has attributes)
|
162
|
+
errors = loaded.class.respond_to?(:attributes) ? validate_type(loaded, context) : validate(loaded, context)
|
162
163
|
raise AttributorException, "Error generating example for #{Attributor.humanize_context(context)}. Errors: #{errors.inspect}" if errors.any?
|
163
164
|
return loaded
|
164
165
|
end
|
@@ -262,11 +262,11 @@ module Attributor
|
|
262
262
|
end
|
263
263
|
|
264
264
|
def self.load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, recurse: false, **_options)
|
265
|
-
context = Array(context)
|
266
265
|
|
267
266
|
return value if value.is_a?(self)
|
268
267
|
return nil if value.nil? && !recurse
|
269
268
|
|
269
|
+
context = Array(context)
|
270
270
|
loaded_value = self.parse(value, context)
|
271
271
|
|
272
272
|
return from_hash(loaded_value, context, recurse: recurse) if keys.any?
|
@@ -282,7 +282,9 @@ module Attributor
|
|
282
282
|
value
|
283
283
|
elsif value.is_a?(::String)
|
284
284
|
decode_json(value, context)
|
285
|
-
elsif value.respond_to?(:
|
285
|
+
elsif value.respond_to?(:to_h)
|
286
|
+
value.to_h
|
287
|
+
elsif value.respond_to?(:to_hash) # Deprecate this in lieu of to_h only?
|
286
288
|
value.to_hash
|
287
289
|
else
|
288
290
|
raise Attributor::IncompatibleTypeError.new(context: context, value_type: value.class, type: self)
|
@@ -301,6 +303,10 @@ module Attributor
|
|
301
303
|
context + ["key(#{key_name.inspect})"]
|
302
304
|
end
|
303
305
|
|
306
|
+
def to_h
|
307
|
+
Attributor.recursive_to_h(@contents)
|
308
|
+
end
|
309
|
+
|
304
310
|
def generate_subcontext(context, key_name)
|
305
311
|
self.class.generate_subcontext(context, key_name)
|
306
312
|
end
|
@@ -548,6 +554,7 @@ module Attributor
|
|
548
554
|
end
|
549
555
|
|
550
556
|
def validate(context = Attributor::DEFAULT_ROOT_CONTEXT)
|
557
|
+
@validating = true
|
551
558
|
context = [context] if context.is_a? ::String
|
552
559
|
|
553
560
|
if self.class.keys.any?
|
@@ -555,6 +562,8 @@ module Attributor
|
|
555
562
|
else
|
556
563
|
self.validate_generic(context)
|
557
564
|
end
|
565
|
+
ensure
|
566
|
+
@validating = false
|
558
567
|
end
|
559
568
|
|
560
569
|
def validate_keys(context)
|
@@ -95,7 +95,9 @@ module Attributor
|
|
95
95
|
value
|
96
96
|
elsif value.is_a?(::String)
|
97
97
|
decode_json(value, context)
|
98
|
-
elsif value.respond_to?(:
|
98
|
+
elsif value.respond_to?(:to_h)
|
99
|
+
value.to_h
|
100
|
+
elsif value.respond_to?(:to_hash) # Deprecate this in lieu of to_h only?
|
99
101
|
value.to_hash
|
100
102
|
else
|
101
103
|
raise Attributor::IncompatibleTypeError, context: context, value_type: value.class, type: self
|
data/lib/attributor/version.rb
CHANGED
data/spec/types/class_spec.rb
CHANGED
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: '5.
|
4
|
+
version: '5.4'
|
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: 2020-
|
12
|
+
date: 2020-05-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: hashie
|
@@ -109,20 +109,6 @@ dependencies:
|
|
109
109
|
- - ">="
|
110
110
|
- !ruby/object:Gem::Version
|
111
111
|
version: '0'
|
112
|
-
- !ruby/object:Gem::Dependency
|
113
|
-
name: backports
|
114
|
-
requirement: !ruby/object:Gem::Requirement
|
115
|
-
requirements:
|
116
|
-
- - "~>"
|
117
|
-
- !ruby/object:Gem::Version
|
118
|
-
version: '3'
|
119
|
-
type: :development
|
120
|
-
prerelease: false
|
121
|
-
version_requirements: !ruby/object:Gem::Requirement
|
122
|
-
requirements:
|
123
|
-
- - "~>"
|
124
|
-
- !ruby/object:Gem::Version
|
125
|
-
version: '3'
|
126
112
|
- !ruby/object:Gem::Dependency
|
127
113
|
name: yardstick
|
128
114
|
requirement: !ruby/object:Gem::Requirement
|