paramore 3.9.3 → 3.9.4

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: b1e0c787e647893bfc2aff716c26dc1451de26f947e372106e1888776926e22d
4
- data.tar.gz: 79a9c035350ec1519c5a1ca546fbaca6c30c97047f101fc94bbdd4eccf1cf593
3
+ metadata.gz: '048043c9ea2f99c0f8233046a6afb9f6710610a5e1bb691f0f4e8e03479e0b20'
4
+ data.tar.gz: 605bc5998bc57401f3b39db585cae073b39254dd11c8cefa98a3ad53b029dc38
5
5
  SHA512:
6
- metadata.gz: 63fa1693e3a7031ed28af6c9eb0d7f960cd1f42562325e4f5bef6e38bb4231f1708b9923872f5d1cb36de17a2cdff2a2556bba850f6d62ceacde007f418bd82c
7
- data.tar.gz: ac1f5eff659d7bbcfb0a723ea4d5a216486e805744aef060780e8d799ac1c859e3111bd0e8b9a52cd71388a7594fe396598ed70c2f28f7eff9135dc4017ab03c
6
+ metadata.gz: 78715e9f134b17502cc9eacac1c6d597cc2e29f4550f223ff2c80eb41a0e50f8e0968830c1542301fb4867a9b0cc89a2dac2e87e16a0fd55b2b5df6e1a70b1f7
7
+ data.tar.gz: bd0ebc180f12c23d3e02860d8f5fbafa9215d3091a08c7fa44b3c92655a1289ee1a8235cd11940bab3a80379b3b87e45bd4f4208b596b9708a541b6120e503e9
@@ -15,18 +15,18 @@ module Paramore
15
15
  end
16
16
 
17
17
  def run
18
- cast(root_field, data, 'root')
18
+ cast(root_field, data, nil)
19
19
  end
20
20
 
21
21
  def cast(field, value, key, namespace = nil)
22
- full_name = [namespace, key].compact.join('.')
22
+ full_name = [namespace, key].select(&:present?).join('.')
23
23
 
24
24
  if value.nil?
25
25
  return field.default if field.nullable? || field.default?
26
26
 
27
27
  raise Paramore::NilParameter, full_name
28
28
  elsif value == ''
29
- return typecast_value(field.type, '', key, full_name) if field.allow_empty?
29
+ return typecast_value(field.type, '', full_name) if field.allow_empty?
30
30
  return field.default if field.default?
31
31
  return if field.nullable?
32
32
 
@@ -35,61 +35,68 @@ module Paramore
35
35
 
36
36
  case field.type
37
37
  when Hash
38
- typecast_hash(field, value || {}, key, full_name)
38
+ typecast_hash(field, value || {}, full_name)
39
39
  when Array
40
- typecast_array(field, value, key, full_name)
40
+ typecast_array(field, value, full_name)
41
41
  else
42
- typecast_value(field.type, value, key, full_name)
42
+ typecast_value(field.type, value, full_name)
43
43
  end
44
44
  end
45
45
 
46
- def typecast_hash(field, hash, key, full_name)
46
+ def typecast_hash(field, hash, full_name)
47
47
  raise Paramore::HashExpected.new(full_name, hash) unless hash.is_a?(Hash)
48
48
 
49
49
  result =
50
50
  if field.wildly_keyed_hash?
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
58
- end
51
+ cast_wild_hash(field, hash, full_name)
59
52
  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
-
65
- field
66
- .type
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
53
+ cast_regular_hash(field, hash, full_name)
74
54
  end
75
55
 
76
56
  field.compact? ? result.compact : result
77
57
  end
78
58
 
79
- def typecast_array(field, array, key, full_name)
59
+ def typecast_array(field, array, full_name)
80
60
  raise Paramore::ArrayExpected.new(full_name, array) unless array.is_a?(Array)
81
61
 
82
- result = array
62
+ result =
63
+ array
83
64
  .reject { |unit| unit.to_s == '' && field.compact? }
84
65
  .map { |unit| cast(Paramore.field(field.type.first, null: true), unit, nil, full_name) }
85
66
 
86
67
  field.compact? ? result.compact : result
87
68
  end
88
69
 
89
- def typecast_value(type, value, key, full_name)
70
+ def typecast_value(type, value, full_name)
90
71
  type.send(Paramore.configuration.type_method_name, value)
91
72
  rescue StandardError => e
92
- raise "The field `#{full_name}` raised \"#{e}\"!"
73
+ raise "Tried casting #{Paramore::ErrorFieldName(full_name)}, but \"#{e}\" was raised!"
74
+ end
75
+
76
+ def cast_wild_hash(field, hash, full_name)
77
+ value_field = field.type.values.first
78
+ key_type = field.type.keys.first
79
+
80
+ hash.to_h do |inner_key, value|
81
+ [
82
+ typecast_value(key_type, inner_key, nil),
83
+ cast(value_field, value, inner_key, full_name)
84
+ ]
85
+ end
86
+ end
87
+
88
+ def cast_regular_hash(field, hash, full_name)
89
+ if options[:no_extra_keys]
90
+ extra = hash.keys - field.type.keys
91
+ raise "Found extra keys in #{Paramore::ErrorFieldName(full_name)}: #{extra}!" if extra.any?
92
+ end
93
+
94
+ field
95
+ .type
96
+ .reject { |inner_key, value_field| missing_and_optional?(hash, inner_key, value_field) }
97
+ .to_h do |inner_key, value_field|
98
+ [inner_key, cast(value_field, hash[inner_key], inner_key, full_name)]
99
+ end
93
100
  end
94
101
 
95
102
  def missing_and_optional?(hash, key, field)
@@ -1,24 +1,24 @@
1
1
  class Paramore::NilParameter < StandardError
2
2
  def initialize(param_name)
3
- super("Received a nil `#{param_name}`, but it's type is non nullable!")
3
+ super("Received a nil #{Paramore::ErrorFieldName(param_name)}, but its type is non nullable!")
4
4
  end
5
5
  end
6
6
 
7
7
  class Paramore::NonField < StandardError
8
8
  def initialize(param_name, type)
9
- super("`#{param_name}` defined as a `#{type.class}`, expected a call of `Paramore.field()`! Perhaps you declared a plain hash instead of Paramore.field({})?")
9
+ super("#{Paramore::ErrorFieldName(param_name)} defined as a `#{type.class}`, expected a call of `Paramore.field()`! Perhaps you declared a plain hash instead of Paramore.field({})?")
10
10
  end
11
11
  end
12
12
 
13
13
  class Paramore::HashExpected < StandardError
14
14
  def initialize(param_name, param)
15
- super("Expected `#{param_name}` to be a hash, received #{param.class} instead!")
15
+ super("Expected #{Paramore::ErrorFieldName(param_name)} to be a hash, received #{param.class} instead!")
16
16
  end
17
17
  end
18
18
 
19
19
  class Paramore::ArrayExpected < StandardError
20
20
  def initialize(param_name, param)
21
- super("Expected `#{param_name}` to be an array, received #{param.class} instead!")
21
+ super("Expected #{Paramore::ErrorFieldName(param_name)} to be an array, received #{param.class} instead!")
22
22
  end
23
23
  end
24
24
 
@@ -27,3 +27,7 @@ class Paramore::HashTooWild < StandardError
27
27
  super("A hash field with a type as key may not contain any more entries! (so, eg.: { String => field } is ok, but { String => field, user_id: field } is not)")
28
28
  end
29
29
  end
30
+
31
+ def Paramore::ErrorFieldName(name)
32
+ name.present? ? "`#{name}`" : 'root field'
33
+ end
@@ -1,7 +1,14 @@
1
1
  module Paramore
2
+ module TypeRegexes
3
+ INTEGER_REGEX = /^-{0,1}\d+$/
4
+ FLOAT_REGEX = /^-{0,1}\d+(\.\d*){0,1}$/
5
+ end
6
+
2
7
  module BigDecimal
3
8
  module_function
4
9
  def [](input)
10
+ raise ArgumentError "#{input} is not a BigDecimal!" unless input.to_s.match?(TypeRegexes::FLOAT_REGEX)
11
+
5
12
  BigDecimal(input)
6
13
  end
7
14
  end
@@ -18,7 +25,7 @@ module Paramore
18
25
  module Float
19
26
  module_function
20
27
  def [](input)
21
- raise ArgumentError "#{input} is not a float!" unless input.to_s.match?(/-{0,1}\d*(\.\d*){0,1}/)
28
+ raise ArgumentError "#{input} is not a Float!" unless input.to_s.match?(TypeRegexes::FLOAT_REGEX)
22
29
 
23
30
  input.to_f
24
31
  end
@@ -27,7 +34,7 @@ module Paramore
27
34
  module Int
28
35
  module_function
29
36
  def [](input)
30
- raise ArgumentError, "#{input} is not an integer!" unless input.to_s.match?(/^-{0,1}\d*$/)
37
+ raise ArgumentError, "#{input} is not an Integer!" unless input.to_s.match?(TypeRegexes::INTEGER_REGEX)
31
38
 
32
39
  input.to_i
33
40
  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.9.3
4
+ version: 3.9.4
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-12-10 00:00:00.000000000 Z
11
+ date: 2021-12-17 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.9.3 !
118
+ Thank you for installing Paramore 3.9.4 !
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