paramore 3.8.4 → 3.9.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/paramore/cast_parameters.rb +58 -37
- data/lib/paramore/types.rb +4 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b1e0c787e647893bfc2aff716c26dc1451de26f947e372106e1888776926e22d
|
4
|
+
data.tar.gz: 79a9c035350ec1519c5a1ca546fbaca6c30c97047f101fc94bbdd4eccf1cf593
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 63fa1693e3a7031ed28af6c9eb0d7f960cd1f42562325e4f5bef6e38bb4231f1708b9923872f5d1cb36de17a2cdff2a2556bba850f6d62ceacde007f418bd82c
|
7
|
+
data.tar.gz: ac1f5eff659d7bbcfb0a723ea4d5a216486e805744aef060780e8d799ac1c859e3111bd0e8b9a52cd71388a7594fe396598ed70c2f28f7eff9135dc4017ab03c
|
@@ -1,78 +1,99 @@
|
|
1
1
|
require_relative 'errors'
|
2
2
|
|
3
3
|
module Paramore
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
class CastParameters
|
5
|
+
attr_accessor :root_field, :data, :options
|
6
|
+
|
7
|
+
def initialize(field, data, options)
|
8
|
+
@root_field = field
|
9
|
+
@data = data
|
10
|
+
@options = options
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.run(field, data, options = {})
|
14
|
+
new(field, data, options).run
|
15
|
+
end
|
16
|
+
|
17
|
+
def run
|
18
|
+
cast(root_field, data, 'root')
|
8
19
|
end
|
9
20
|
|
10
|
-
def cast(field, value,
|
21
|
+
def cast(field, value, key, namespace = nil)
|
22
|
+
full_name = [namespace, key].compact.join('.')
|
23
|
+
|
11
24
|
if value.nil?
|
12
|
-
if field.nullable? || field.default?
|
13
|
-
|
14
|
-
|
15
|
-
raise Paramore::NilParameter, name
|
16
|
-
end
|
25
|
+
return field.default if field.nullable? || field.default?
|
26
|
+
|
27
|
+
raise Paramore::NilParameter, full_name
|
17
28
|
elsif value == ''
|
18
|
-
if field.allow_empty?
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
return
|
24
|
-
else
|
25
|
-
raise Paramore::NilParameter, name
|
26
|
-
end
|
29
|
+
return typecast_value(field.type, '', key, full_name) if field.allow_empty?
|
30
|
+
return field.default if field.default?
|
31
|
+
return if field.nullable?
|
32
|
+
|
33
|
+
raise Paramore::NilParameter, full_name
|
27
34
|
end
|
28
35
|
|
29
36
|
case field.type
|
30
37
|
when Hash
|
31
|
-
typecast_hash(field, value || {},
|
38
|
+
typecast_hash(field, value || {}, key, full_name)
|
32
39
|
when Array
|
33
|
-
typecast_array(field, value,
|
40
|
+
typecast_array(field, value, key, full_name)
|
34
41
|
else
|
35
|
-
typecast_value(field.type, value,
|
42
|
+
typecast_value(field.type, value, key, full_name)
|
36
43
|
end
|
37
44
|
end
|
38
45
|
|
39
|
-
def typecast_hash(field, hash,
|
40
|
-
raise Paramore::HashExpected.new(
|
46
|
+
def typecast_hash(field, hash, key, full_name)
|
47
|
+
raise Paramore::HashExpected.new(full_name, hash) unless hash.is_a?(Hash)
|
41
48
|
|
42
49
|
result =
|
43
50
|
if field.wildly_keyed_hash?
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
51
|
+
[field.type.values.first, field.type.keys.first].then do |value_field, key_type|
|
52
|
+
hash.to_h do |inner_key, value|
|
53
|
+
[
|
54
|
+
typecast_value(key_type, inner_key, nil, nil),
|
55
|
+
cast(value_field, value, inner_key, full_name)
|
56
|
+
]
|
57
|
+
end
|
48
58
|
end
|
49
59
|
else
|
60
|
+
if options[:no_extra_keys]
|
61
|
+
extra = hash.keys - field.type.keys
|
62
|
+
raise "Found extra keys in `#{full_name}`: #{extra}!" if extra.any?
|
63
|
+
end
|
64
|
+
|
50
65
|
field
|
51
66
|
.type
|
52
|
-
.reject { |
|
53
|
-
.to_h
|
67
|
+
.reject { |inner_key, value_field| missing_and_optional?(hash, inner_key, value_field) }
|
68
|
+
.to_h do |inner_key, value_field|
|
69
|
+
[
|
70
|
+
inner_key,
|
71
|
+
cast(value_field, hash[inner_key], inner_key, full_name)
|
72
|
+
]
|
73
|
+
end
|
54
74
|
end
|
55
75
|
|
56
|
-
|
57
76
|
field.compact? ? result.compact : result
|
58
77
|
end
|
59
78
|
|
60
|
-
def typecast_array(field, array,
|
61
|
-
raise Paramore::ArrayExpected.new(
|
79
|
+
def typecast_array(field, array, key, full_name)
|
80
|
+
raise Paramore::ArrayExpected.new(full_name, array) unless array.is_a?(Array)
|
62
81
|
|
63
82
|
result = array
|
64
83
|
.reject { |unit| unit.to_s == '' && field.compact? }
|
65
|
-
.map { |unit| cast(Paramore.field(field.type.first, null: true), unit) }
|
84
|
+
.map { |unit| cast(Paramore.field(field.type.first, null: true), unit, nil, full_name) }
|
66
85
|
|
67
86
|
field.compact? ? result.compact : result
|
68
87
|
end
|
69
88
|
|
70
|
-
def typecast_value(type, value,
|
89
|
+
def typecast_value(type, value, key, full_name)
|
71
90
|
type.send(Paramore.configuration.type_method_name, value)
|
91
|
+
rescue StandardError => e
|
92
|
+
raise "The field `#{full_name}` raised \"#{e}\"!"
|
72
93
|
end
|
73
94
|
|
74
|
-
def missing_and_optional?(hash,
|
75
|
-
!hash.key?(
|
95
|
+
def missing_and_optional?(hash, key, field)
|
96
|
+
!hash.key?(key) && !field.required?
|
76
97
|
end
|
77
98
|
end
|
78
99
|
end
|
data/lib/paramore/types.rb
CHANGED
@@ -18,6 +18,8 @@ module Paramore
|
|
18
18
|
module Float
|
19
19
|
module_function
|
20
20
|
def [](input)
|
21
|
+
raise ArgumentError "#{input} is not a float!" unless input.to_s.match?(/-{0,1}\d*(\.\d*){0,1}/)
|
22
|
+
|
21
23
|
input.to_f
|
22
24
|
end
|
23
25
|
end
|
@@ -25,6 +27,8 @@ module Paramore
|
|
25
27
|
module Int
|
26
28
|
module_function
|
27
29
|
def [](input)
|
30
|
+
raise ArgumentError, "#{input} is not an integer!" unless input.to_s.match?(/^-{0,1}\d*$/)
|
31
|
+
|
28
32
|
input.to_i
|
29
33
|
end
|
30
34
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: paramore
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.9.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lukas Kairevičius
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-10
|
11
|
+
date: 2021-12-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec-rails
|
@@ -115,7 +115,7 @@ licenses:
|
|
115
115
|
- MIT
|
116
116
|
metadata: {}
|
117
117
|
post_install_message: |
|
118
|
-
Thank you for installing Paramore 3.
|
118
|
+
Thank you for installing Paramore 3.9.3 !
|
119
119
|
From the command line you can run `paramore` to generate a configuration file
|
120
120
|
|
121
121
|
More details here : https://github.com/lumzdas/paramore/blob/master/README.md
|