error_builder 0.3.0 → 0.4.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: fef4923015c619400c0faaeba5412188078ad963740c10f8dcfced24a65e3a8e
4
- data.tar.gz: 77857ce23ee12bed15d4b53e3a9c47fcff761f1b51bbfb94309e2502db8af1b7
3
+ metadata.gz: 18b518b8436e2ef66d0bb88f820bef052f36df55a4e41bb28324fe0aebe237d8
4
+ data.tar.gz: 3c2430d0678d632349469410999bb82825ae5e07c1f36e185857222864e59a59
5
5
  SHA512:
6
- metadata.gz: 18b4a185c1d4b2f59405561539700b82e4a516ed5915d7a84ec63cef36c1b3f100c07fffe596a28a878096c657198a230c196f2f3936e2119e5ceb53c0469461
7
- data.tar.gz: 3a68abedfdb7a80e72aec329afd3bb735153f7e5f4d5fbb8c55accbcca4f5e7887d2ee8750750da4e22c52b88f0fc8fc29f3cdbb19da2dc3ca68f6eb23e71c29
6
+ metadata.gz: 3d67f22c74f876da70c19d27267aa4036c796bed1737a18f3ca3da380e446f90705cf5e19a246bba2314bf8c126346545f385102e9a9c325b958205d508c1a3b
7
+ data.tar.gz: 91deb51af311a62b39edec12ddfc6440e7c02c8f13227d8cc7922ce5c59e0e198b4f728dc1ba085efd1619bdab6df7c107b4fd6173283f09daa4112cda6b570a
data/CHANGELOG.md CHANGED
@@ -1,6 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
- ## [0.3.0] - 2025-04.06
3
+ ## [0.4.0] - 2025-04-13
4
+ - Added support of custom format classes.
5
+
6
+ ## [0.3.0] - 2025-04-06
4
7
 
5
8
  - Changed Array and Hash formats.
6
9
  - Added support for nested and flat error structures in both hash and array formats.
data/README.md CHANGED
@@ -34,7 +34,7 @@ If you have to use in Rails:
34
34
  3. You can configure the gem by using the ErrorBuilder.configure block:
35
35
  ```ruby
36
36
  ErrorBuilder.configure do |config|
37
- config.format = :hash # Supported formats: :hash, :array
37
+ config.format = :hash # Supported formats: :hash, :array, custom class format
38
38
  config.message_format = :string # Supported formats: :string, :array
39
39
  end
40
40
  ```
@@ -95,6 +95,24 @@ my_service.errors.to_h #=> { "user" => { "locations" => { 0 => { "na
95
95
  my_service.errors.to_h(flat: true) #=> { "user.locations[0].name" => ["must be present"] }
96
96
  ```
97
97
 
98
+ #### Using custom formats
99
+
100
+ ```ruby
101
+ class CustomFormat
102
+ def initialize(errors, **)
103
+ @errors = errors
104
+ end
105
+
106
+ def to_h
107
+ @errors.map { |error| "#{error.key} #{error.message.join(", ")}" }
108
+ end
109
+ end
110
+
111
+ errors = ErrorBuilder::Engine.new(format: CustomFormat)
112
+ errors.add(:base, "Something went wrong")
113
+ errors.to_h #=> ["base Something went wrong"]
114
+ ```
115
+
98
116
  ## Development
99
117
 
100
118
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. Then, eun `rake rubocop` to run the rubocop. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -17,14 +17,11 @@ module ErrorBuilder
17
17
  end
18
18
 
19
19
  def to_h(flat: false)
20
- case format
21
- when :array
22
- Formats::Array.new(@errors, flat:).to_h
23
- when :hash
24
- Formats::Hash.new(@errors, flat:).to_h
25
- else
26
- raise ArgumentError, "Unsupported format: #{format}"
27
- end
20
+ formatter.new(errors, flat:).to_h
21
+ end
22
+
23
+ def formatter
24
+ FormatResolver.new(format).formatter
28
25
  end
29
26
  end
30
27
  end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ErrorBuilder
4
+ class FormatResolver
5
+ attr_reader :format
6
+
7
+ def initialize(format)
8
+ @format = format
9
+ end
10
+
11
+ def formatter
12
+ return built_in_formatter unless format.is_a?(Class)
13
+
14
+ format
15
+ end
16
+
17
+ def built_in_formatter
18
+ case format
19
+ when :array
20
+ Formats::Array
21
+ when :hash
22
+ Formats::Hash
23
+ else
24
+ raise ArgumentError, "Unsupported format: #{format}"
25
+ end
26
+ end
27
+ end
28
+ end
@@ -5,7 +5,7 @@ module ErrorBuilder
5
5
  class Base
6
6
  attr_reader :errors, :flat
7
7
 
8
- def initialize(errors, flat:)
8
+ def initialize(errors, flat: false)
9
9
  @errors = errors
10
10
  @flat = flat
11
11
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ErrorBuilder
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.0"
5
5
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: error_builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mykhailo Marusyk
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-04-06 00:00:00.000000000 Z
10
+ date: 2025-04-13 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: zeitwerk
@@ -43,6 +43,7 @@ files:
43
43
  - lib/error_builder/configuration.rb
44
44
  - lib/error_builder/engine.rb
45
45
  - lib/error_builder/error.rb
46
+ - lib/error_builder/format_resolver.rb
46
47
  - lib/error_builder/formats/array.rb
47
48
  - lib/error_builder/formats/base.rb
48
49
  - lib/error_builder/formats/hash.rb