salestation 5.0.2 → 5.1.0

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: 45624d622f8661658f1549308516bf9600bad88764a1e6682ea9e585c95be059
4
- data.tar.gz: b10067eda3d00fd6da5648f0a396f558e8c6122968b8393e6934fd7053a34e6a
3
+ metadata.gz: 43663c5f6d78d7238e66e9ac88a06a49facfcaf45a329534e44e20891ea1baa9
4
+ data.tar.gz: 97661dd29a53ab956cbe0c78522b382d052bd4c6f339208fe126f7d3c51f8526
5
5
  SHA512:
6
- metadata.gz: 7eb34e5d245f616f9db8e8a64f3df426ea80982bd44a00933cb681b5c1fc5e818e4b8b6ff7dcdf177f841448fd50d8ccf1eeb62a65619fceb5cd25e893007966
7
- data.tar.gz: a6c636b8b308f57a2c23c99d63a2099b05f6dc89e133caa29f98a35920d971cd9e3905176c1075fa891e4442d164be9050f8e57f3ef0c91f6bd7021d3170a423
6
+ metadata.gz: ee4cf2f592d41548e187277789ec6d3484748dc7cc152f3a9b4e4b63b438314e5fe96ab3aeb92dbd4729a485b4d9e73b518904459309b19f3da7a507595100ce
7
+ data.tar.gz: 22a1c5415ded67ebc22c5f67ef6332706571a156000aac7e5195101c73be14dc950b5f208fb24b19e27c94b70c3c854dc8a5da485a01b030d578e9ccaac92c57
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ .rspec_persistence
@@ -15,24 +15,24 @@ module Salestation
15
15
  @field_error_messages = {}
16
16
  end
17
17
 
18
- def on(field)
19
- @fields << field
18
+ def on(*nested_fields)
19
+ @fields << nested_fields
20
20
  self
21
21
  end
22
22
 
23
23
  def with_type(*types)
24
- @field_error_types[@fields.last] = types
24
+ @field_error_types[field_to_key(@fields.last)] = types
25
25
  self
26
26
  end
27
27
 
28
28
  def with_message(*messages)
29
- @field_error_messages[@fields.last] = messages
29
+ @field_error_messages[field_to_key(@fields.last)] = messages
30
30
  self
31
31
  end
32
32
 
33
33
  def matches?(actual)
34
34
  @fields.all? do |field|
35
- check_field_exists(field, actual) &&
35
+ check_field_exists(actual, *field) &&
36
36
  check_field_error_types(field, actual) &&
37
37
  check_field_error_messages(field, actual)
38
38
  end
@@ -40,24 +40,45 @@ module Salestation
40
40
 
41
41
  private
42
42
 
43
- def check_field_exists(field, actual)
44
- actual[:error_details].key?(field)
43
+ def check_field_exists(actual, field, *nested_fields)
44
+ actual[:error_details].key?(field) &&
45
+ (
46
+ nested_fields.empty? ||
47
+ actual[:error_details][field].any? do |nested_hash|
48
+ check_field_exists(nested_hash, *nested_fields)
49
+ end
50
+ )
45
51
  end
46
52
 
47
53
  def check_field_error_types(field, actual)
48
- return true unless @field_error_types[field]
54
+ return true unless @field_error_types[field_to_key(field)]
49
55
 
50
- actual_error_types = actual[:error_details].fetch(field).map { |f| f.fetch(:type) }
51
-
52
- (@field_error_types[field] - actual_error_types).empty?
56
+ actual_error_types = fetch_field_attribute_nested(actual, :type, *field)
57
+ (@field_error_types[field_to_key(field)] - actual_error_types).empty?
53
58
  end
54
59
 
55
60
  def check_field_error_messages(field, actual)
56
- return true unless @field_error_messages[field]
61
+ return true unless @field_error_messages[field_to_key(field)]
62
+
63
+ actual_error_messages = fetch_field_attribute_nested(actual, :message, *field)
64
+ (@field_error_messages[field_to_key(field)] - actual_error_messages).empty?
65
+ end
57
66
 
58
- actual_error_messages = actual[:error_details].fetch(field).map { |f| f.fetch(:message) }
59
67
 
60
- (@field_error_messages[field] - actual_error_messages).empty?
68
+ def fetch_field_attribute_nested(actual, attribute, field, *nested_fields)
69
+ if nested_fields.empty?
70
+ actual[:error_details].fetch(field).map { |f| f.fetch(attribute) }
71
+ else
72
+ actual[:error_details][field]
73
+ .lazy
74
+ .map { |error_hash| fetch_field_attribute_nested(error_hash, attribute, *nested_fields) }
75
+ .reject(&:nil?)
76
+ .first
77
+ end
78
+ end
79
+
80
+ def field_to_key(fields)
81
+ fields.join('->').to_sym
61
82
  end
62
83
  end
63
84
  end
@@ -44,6 +44,23 @@ module Salestation
44
44
  # .on(:phone_number).with_type(Glia::Errors::INVALID_FORMAT_ERROR)
45
45
  # )
46
46
  #
47
+ # also has support for nested errors:
48
+ # nesting can be represented by calling `on` method with multiple arguments, for example when
49
+ # you have a hash like
50
+ # {
51
+ # nested: {
52
+ # name: ''
53
+ # }
54
+ # }
55
+ # Then you can call it like glia_input_validation_error.on(:nested, :name)
56
+ #
57
+ # expect(result).to be_failure
58
+ # .with_invalid_input
59
+ # .containing(
60
+ # glia_input_validation_error
61
+ # .on(:nested, :name).with_type(Glia::Errors::INVALID_VALUE_ERROR)
62
+ # )
63
+ #
47
64
  # Everything after be_failure is optional. You could also use `.containing`
48
65
  # multiple times like this:
49
66
  #
data/salestation.gemspec CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "salestation"
7
- spec.version = "5.0.2"
7
+ spec.version = "5.1.0"
8
8
  spec.authors = ["Glia TechMovers"]
9
9
  spec.email = ["techmovers@glia.com"]
10
10
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: salestation
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.0.2
4
+ version: 5.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Glia TechMovers
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-07-04 00:00:00.000000000 Z
11
+ date: 2022-08-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler