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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0cc67c79a09ee03ca89162f574ff3b6fda2b24bedc6e3a20ec5c0e780e9999c1
4
- data.tar.gz: 24895561218cf93fb309b31c40b370ddd0cd0899986342d0b1d847f11459525f
3
+ metadata.gz: b1e0c787e647893bfc2aff716c26dc1451de26f947e372106e1888776926e22d
4
+ data.tar.gz: 79a9c035350ec1519c5a1ca546fbaca6c30c97047f101fc94bbdd4eccf1cf593
5
5
  SHA512:
6
- metadata.gz: 28cf57ff69601f5a351a817b104111595bf1fb7a05cefa14066701710c389b7dfef68c07098f88865e2aa0db358ce4987c9a71aa9b76553f9fcd9817f851c1d2
7
- data.tar.gz: 0ce7299a531ee49b3695c542873932326561bb8ae6b7af812071b41f1ac45ef11e89e3004c5a628005afdc9e3884c47d5b632656b889b4e6a6bcbc20dc761a5c
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
- module CastParameters
5
- module_function
6
- def run(field, data)
7
- cast(field, data, 'data')
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, name = nil)
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
- return field.default
14
- else
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
- return typecast_value(field.type, '', name)
20
- elsif field.default?
21
- return field.default
22
- elsif field.nullable?
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 || {}, name)
38
+ typecast_hash(field, value || {}, key, full_name)
32
39
  when Array
33
- typecast_array(field, value, name)
40
+ typecast_array(field, value, key, full_name)
34
41
  else
35
- typecast_value(field.type, value, name)
42
+ typecast_value(field.type, value, key, full_name)
36
43
  end
37
44
  end
38
45
 
39
- def typecast_hash(field, hash, name)
40
- raise Paramore::HashExpected.new(name, hash) unless hash.is_a?(Hash)
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
- value_field = field.type.values.first
45
- key_type = field.type.keys.first
46
- hash.to_h do |name, value|
47
- [typecast_value(key_type, name, nil), cast(value_field, value, name)]
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 { |name, value_field| missing_and_optional?(hash, name, value_field) }
53
- .to_h { |name, value_field| [name, cast(value_field, hash[name], name)] }
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, name)
61
- raise Paramore::ArrayExpected.new(name, array) unless array.is_a?(Array)
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, name)
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, name, field)
75
- !hash.key?(name) && !field.required?
95
+ def missing_and_optional?(hash, key, field)
96
+ !hash.key?(key) && !field.required?
76
97
  end
77
98
  end
78
99
  end
@@ -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.8.4
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-25 00:00:00.000000000 Z
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.8.4 !
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