gourami 0.5.0 → 1.0.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: 98ec9c2d757ec710e65dd5aa0662530f0f92363b72d56edce9fecebe85d0d800
4
- data.tar.gz: 80842660cd5e2ed28a244414ef8a8f8f091d111d8fd2ba614bcec52dc0efe807
3
+ metadata.gz: 6ed05ee6d0864e3a51c4c29719f7cc6d6b6ca8ba3f360fc8ad950f026db5b69f
4
+ data.tar.gz: 665904a584c35c5ad55f710a49bed160f039706772f1f8ccd485ccd2351a8ebc
5
5
  SHA512:
6
- metadata.gz: 94897c3b427a7528020c6df8b657c193e88488b52610bf853d2e3fffda962ba1e0ba642ba7c72e2c9bb6ecf234518372910f733bca453d72a2b72c4ee469cb55
7
- data.tar.gz: 12d3a6215c2fc337aa03af6d0818b916879aebe77cb96a21aed65d47ca7385a68eb096cc81dc22ff39ca3e187260ffd0a0f55060a4fb38db948eeb5a673a0e87
6
+ metadata.gz: f830e299356968c7248b5d4bc02294299e91929a5e7fb4db7e06e25ec12a581393ce3bbcfcaafe889e65bd67e51406f3a3ed1d4b2b26e375af9c304d8befc205
7
+ data.tar.gz: 6c55ad5a92607e4387c4221ee2597ab091ab436b70196afbc0acf9de3ad9e2482bc19419ee384e49420478d440c3b45e2d1e4e68e56a220e0653ee8fce98d9fb
data/README.md CHANGED
@@ -252,7 +252,9 @@ end
252
252
 
253
253
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
254
254
 
255
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
255
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
256
+
257
+ To add another gem owner to gourami gem `gem owner --add john.smith@example.com gourami`
256
258
 
257
259
  ## Contributing
258
260
 
@@ -91,6 +91,28 @@ module Gourami
91
91
  resource_errors.values.flat_map(&:values).map(&:values).flatten.any?
92
92
  end
93
93
 
94
+ # Replace the existing resource errors with the provided errors Hash.
95
+ #
96
+ # @param new_resource_errors [Hash<Symbol, Hash<Symbol, Hash<Symbol, Array>>>]
97
+ #
98
+ # @return [Hash<Symbol, Hash<Symbol, Hash<Symbol, Array>>>]
99
+ def clear_and_set_resource_errors(new_resource_errors)
100
+ new_resource_errors = new_resource_errors.dup
101
+ resource_errors.clear
102
+ resource_errors.merge!(new_resource_errors)
103
+
104
+ resource_errors
105
+ end
106
+
107
+ def handle_validation_error(error)
108
+ super(error)
109
+ clear_and_set_resource_errors(error.resource_errors) unless error.resource_errors.nil?
110
+ end
111
+
112
+ def raise_validate_errors
113
+ raise ValidationError.new(errors, resource_errors)
114
+ end
115
+
94
116
  end
95
117
  end
96
118
  end
@@ -9,20 +9,39 @@ module Gourami
9
9
  end
10
10
  end
11
11
 
12
+ def self.stringify_resource_errors(resource_errors)
13
+ [].tap do |array|
14
+ resource_errors.each do |resource_namespace, resource_namespace_errors|
15
+ resource_namespace_errors.each do |resource_uid, resource_uid_errors|
16
+ resource_uid_errors.each do |attribute_name, error|
17
+ array.push("#{resource_namespace}:#{resource_uid}:#{attribute_name}: #{error}")
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+
12
24
  # !@attribute [r] errors
13
25
  # @return [Hash<Symbol, Array>]
14
26
  attr_reader :errors
15
27
 
28
+ # !@attribute [r] resource_errors
29
+ # @return [Hash<Symbol, Hash<Symbol, Hash<Symbol, Array>>>]
30
+ attr_reader :resource_errors
31
+
16
32
  # Initialize the Gourami::ValidationError.
17
33
  #
18
34
  # @param errors [Hash<Symbol, Array>]
19
- def initialize(errors)
35
+ # @param resource_errors [Hash<Symbol, Hash<Symbol, Hash<Symbol, Array>>>]
36
+ def initialize(errors, resource_errors = {})
37
+ @resource_errors = resource_errors
20
38
  @errors = errors
39
+
21
40
  super(message)
22
41
  end
23
42
 
24
43
  def message
25
- @message ||= "Validation failed with errors: #{stringify_errors.join("\n")}"
44
+ @message ||= stringify_all_errors
26
45
  end
27
46
 
28
47
  private
@@ -31,5 +50,16 @@ module Gourami
31
50
  ValidationError.stringify_errors(errors)
32
51
  end
33
52
 
53
+ def stringify_resource_errors
54
+ ValidationError.stringify_resource_errors(resource_errors)
55
+ end
56
+
57
+ def stringify_all_errors
58
+ messages = []
59
+ messages << "Validation failed with errors: #{stringify_errors.join("\n")}" unless errors.nil? || errors.empty?
60
+ messages << "Validation failed with resource errors: #{stringify_resource_errors.join("\n")}" unless resource_errors.nil? || resource_errors.empty?
61
+ messages.join("\n")
62
+ end
63
+
34
64
  end
35
65
  end
@@ -15,11 +15,16 @@ module Gourami
15
15
  # @raise [Gourami::ValidationError]
16
16
  def perform!
17
17
  if valid?
18
- returned = perform
18
+ begin
19
+ returned = perform
20
+ rescue Gourami::ValidationError => error
21
+ handle_validation_error(error)
22
+ raise
23
+ end
19
24
  end
20
25
 
21
26
  if any_errors?
22
- raise ValidationError.new(errors)
27
+ raise_validate_errors
23
28
  end
24
29
 
25
30
  returned
@@ -46,9 +51,9 @@ module Gourami
46
51
 
47
52
  # Replace the existing errors with the provided errors Hash.
48
53
  #
49
- # @param new_errors [Hash<Symbol, nil>, Array<Symbol, String>]
54
+ # @param new_errors Hash<Symbol, Array>
50
55
  #
51
- # @return [Hash<Symbol, nil>, Array<Symbol, String>]
56
+ # @return Hash<Symbol, Array>
52
57
  def clear_and_set_errors(new_errors)
53
58
  new_errors = new_errors.dup
54
59
  errors.clear
@@ -57,6 +62,14 @@ module Gourami
57
62
  errors
58
63
  end
59
64
 
65
+ def raise_validate_errors
66
+ raise ValidationError.new(errors)
67
+ end
68
+
69
+ def handle_validation_error(error)
70
+ clear_and_set_errors(error.errors) unless error.errors.nil?
71
+ end
72
+
60
73
  # Return true if there given attribute has any errors.
61
74
  def attribute_has_errors?(attribute_name)
62
75
  errors[attribute_name.to_sym].any?
@@ -1,3 +1,3 @@
1
1
  module Gourami
2
- VERSION = "0.5.0".freeze
2
+ VERSION = "1.0.0".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gourami
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - TSMMark
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-10-30 00:00:00.000000000 Z
11
+ date: 2019-12-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
@@ -118,8 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
118
  - !ruby/object:Gem::Version
119
119
  version: '0'
120
120
  requirements: []
121
- rubyforge_project:
122
- rubygems_version: 2.7.7
121
+ rubygems_version: 3.0.3
123
122
  signing_key:
124
123
  specification_version: 4
125
124
  summary: Keep your Routes, Controllers and Models thin.