metaractor 3.3.5 → 3.4.1

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: ba4a53b90f455fc6e2940aaabe62e2a88b64657feb57cba562b7456c68ed5cc8
4
- data.tar.gz: c0f76bccfd5ad86a9ed9a2e0212b84512ceb56ab26426c0ea67251531a7e7498
3
+ metadata.gz: 54f810c7379560bdedc30b5b748fdfea64ee6ee6040b021f1c7315ebd82d6d25
4
+ data.tar.gz: 1b78f912f057e8c5bc8e9490e7c427f9203c240442561efc2acaecc72a16fa80
5
5
  SHA512:
6
- metadata.gz: 0be2757b00ef748691d6d62b3021e6ce2fe481826d042da3ab27da93ba8ce1eaed4aabe73c069082683409cccdfd6510b3b8a137689ecc87ac1a3f641ed3892d
7
- data.tar.gz: '090b6cba71a3b1a48c322ffb110dcb09b6eb77f87c6ac153b3a143c13e1525310fe3694aa50435a18dd5bc8b9ceb120087f9a8b65d6f74eee0a8c0c96b4820c7'
6
+ metadata.gz: cf8e038791c2611ed349dc6265cf349a68989f8aa227abc5e6c1458f50afc1f3b5c71dbfb9a12e8e56dbedbadfe8600fcb1f96e05533804393d0760c7dbd9821
7
+ data.tar.gz: 2909274be834f5621a739893803d1e5551e5aeebc9da404e216e07cb9b5947b725a78fbb5069b085c24f0c0ba4811026e370c92bb25a8099ef9d2ee4916a248f
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 3.4.4
1
+ 4.0.3
data/README.md CHANGED
@@ -12,13 +12,14 @@ gem 'metaractor'
12
12
 
13
13
  And then execute:
14
14
 
15
- $ bundle
15
+ bundle
16
16
 
17
17
  Or install it yourself as:
18
18
 
19
- $ gem install metaractor
19
+ gem install metaractor
20
20
 
21
21
  ## Usage
22
+
22
23
  ```ruby
23
24
  class HighFiveUser
24
25
  include Metaractor
@@ -52,6 +53,7 @@ result.error_messages
52
53
  See Interactor's [README](https://github.com/collectiveidea/interactor/blob/master/README.md) for more information.
53
54
 
54
55
  ### Configuration
56
+
55
57
  Metaractor is meant to be extensible (hence the 'meta'). You can add additional modules in the following way:
56
58
 
57
59
  ```ruby
@@ -64,29 +66,37 @@ end
64
66
  ```
65
67
 
66
68
  ### Required Parameters
69
+
67
70
  Metaractor supports complex required parameter statements and you can chain these together in any manner using `and`, `or`, and `xor`.
71
+
68
72
  ```ruby
69
73
  required and: [:token, or: [:recipient_id, :recipient] ]
70
74
  ```
71
75
 
72
76
  You can also mark a parameter as required with the `required` option:
77
+
73
78
  ```ruby
74
79
  parameter :user, required: true
75
80
  ```
76
81
 
77
82
  ### Optional Parameters
83
+
78
84
  As optional parameters have no enforcement, they are merely advisory.
85
+
79
86
  ```ruby
80
87
  optional :enable_logging
81
88
  ```
82
89
 
83
90
  ### Parameter Options
91
+
84
92
  Metaractor supports arbitrary parameter options. The following are currently built in.
85
93
  Note that you can specify a block of `required` or `optional` parameters and then use
86
94
  `parameter` or `parameters` to add options to one or more of them.
87
95
 
88
96
  #### Skipping Blank Parameter Removal
97
+
89
98
  By default Metaractor removes blank values that are passed in. You may skip this behavior on a per-parameter basis:
99
+
90
100
  ```ruby
91
101
  parameter :name, allow_blank: true
92
102
  ```
@@ -94,28 +104,35 @@ parameter :name, allow_blank: true
94
104
  You may check to see if a parameter exists via `context.has_key?`.
95
105
 
96
106
  #### Default Values
107
+
97
108
  You can specify a default value for a parameter:
109
+
98
110
  ```ruby
99
111
  optional :role, default: :user
100
112
  ```
101
113
 
102
114
  This works with `allow_blank` and can also be anything that responds to `#call`.
115
+
103
116
  ```ruby
104
117
  parameter :role, allow_blank: true, default: -> { context.default_role }
105
118
  ```
106
119
 
107
120
  #### Typecasting/Coersion
121
+
108
122
  You can supply Metaractor with a callable that will typecast incoming parameters:
123
+
109
124
  ```ruby
110
125
  optional :needs_to_be_a_string, type: ->(value) { value.to_s }
111
126
  ```
112
127
 
113
128
  You can also configure Metaractor with named types and use them:
129
+
114
130
  ```ruby
115
131
  Metaractor.configure do |config|
116
132
  config.register_type(:boolean, ->(value) { ActiveModel::Type::Boolean.new.cast(value) })
117
133
  end
118
134
  ```
135
+
119
136
  ```ruby
120
137
  required :is_awesome, type: :boolean
121
138
  ```
@@ -123,7 +140,9 @@ required :is_awesome, type: :boolean
123
140
  **Note**: Typecasters will _not_ be called on `nil` values.
124
141
 
125
142
  ### Custom Validation
143
+
126
144
  Metaractor supports doing custom validation before any user supplied before_hooks run.
145
+
127
146
  ```ruby
128
147
  validate_parameters do
129
148
  if context.foo == :bar
@@ -137,6 +156,7 @@ end
137
156
  ```
138
157
 
139
158
  If you need to require a parameter from a `before_hook` for any reason, use the bang version of the method:
159
+
140
160
  ```ruby
141
161
  before do
142
162
  # Be careful with this approach as some user code may run before the parameter validation
@@ -145,7 +165,9 @@ end
145
165
  ```
146
166
 
147
167
  ### Structured Errors
168
+
148
169
  As of v2.0.0, metaractor supports structured errors.
170
+
149
171
  ```ruby
150
172
  class UpdateUser
151
173
  include Metaractor
@@ -202,7 +224,9 @@ result.errors.to_h
202
224
  ```
203
225
 
204
226
  ### I18n
227
+
205
228
  As of v3.0.0, metaractor supports i18n along with structured errors.
229
+
206
230
  ```ruby
207
231
  module Users
208
232
  class UpdateUser
@@ -225,6 +249,7 @@ end
225
249
  ```
226
250
 
227
251
  Locale:
252
+
228
253
  ```yaml
229
254
  en:
230
255
  errors:
@@ -246,10 +271,12 @@ You can see that above with the `users` key in the locale.
246
271
 
247
272
  The i18n integration will walk its way from the most specific message to the least specific one, stopping at the first one it can find.
248
273
  We currently expose the following variables for use in the message:
274
+
249
275
  - `error_key`: the error we added (ex: `blank` or `invalid_configuration`)
250
276
  - `parameter`: the name of the parameter
251
277
 
252
278
  You can also use this feature to work with machine readable keys:
279
+
253
280
  ```ruby
254
281
  result = Users::UpdateUser.call
255
282
  if result.failure? &&
@@ -260,7 +287,9 @@ end
260
287
  ```
261
288
 
262
289
  ### Spec Helpers
290
+
263
291
  Enable the helpers and/or matchers:
292
+
264
293
  ```ruby
265
294
  RSpec.configure do |config|
266
295
  config.include Metaractor::Spec::Helpers
@@ -269,7 +298,9 @@ end
269
298
  ```
270
299
 
271
300
  #### Helpers
301
+
272
302
  - `context_creator`
303
+
273
304
  ```ruby
274
305
  # context_creator(error_message: nil, error_messages: [], errors: [], valid: nil, invalid: nil, success: nil, failure: nil, **attributes)
275
306
 
@@ -300,7 +331,9 @@ context_creator(
300
331
  ```
301
332
 
302
333
  #### Matchers
334
+
303
335
  - `include_errors`
336
+
304
337
  ```ruby
305
338
  result = context_creator(
306
339
  errors: {
@@ -320,7 +353,9 @@ expect(result).to include_errors('user.title cannot be blank')
320
353
  ```
321
354
 
322
355
  ### Hash Formatting
356
+
323
357
  Metaractor customizes the output for `Metaractor::Errors#inspect` and `Interactor::Failure`:
358
+
324
359
  ```
325
360
  Interactor::Failure:
326
361
  Errors:
@@ -334,6 +369,7 @@ Interactor::Failure:
334
369
  ```
335
370
 
336
371
  You can further customize the hash formatting:
372
+
337
373
  ```ruby
338
374
  Metaractor.configure do |config|
339
375
  # Configure Metaractor to use awesome_print
@@ -342,22 +378,21 @@ end
342
378
  ```
343
379
 
344
380
  ### Further Reading
381
+
345
382
  For more examples of all of the above approaches, please see the specs.
346
383
 
347
384
  ## Development
348
385
 
349
- - `docker compose build --pull`
350
- - `docker compose run --rm metaractor` to run specs
351
-
352
- or with the Deskfile loaded:
386
+ - `flox pull ryansch/ruby-4-0`
387
+ - `flox activate`
388
+ - `bundle`
353
389
  - `rspec spec`
354
390
 
355
391
  To release a new version:
392
+
356
393
  - Update the version number in `version.rb` and commit the result.
357
- - `docker compose build --pull`
358
- - `docker compose run --rm release`
394
+ - Trigger the Release workflow.
359
395
 
360
396
  ## Contributing
361
397
 
362
- Bug reports and pull requests are welcome on GitHub at https://github.com/metaractor/metaractor.
363
-
398
+ Bug reports and pull requests are welcome on GitHub at <https://github.com/metaractor/metaractor>.
@@ -12,5 +12,15 @@ module Metaractor
12
12
  context.fail_from_context(context: e.context)
13
13
  raise
14
14
  end
15
+
16
+ def run(...)
17
+ run!(...)
18
+ rescue Interactor::Failure
19
+ # Intentionally rescue all Failures as we're
20
+ # handling child errors by failing the parents
21
+ # instead of allowing the child to raise
22
+ # all of the way up.
23
+ # Needed since Interactor v3.2.0.
24
+ end
15
25
  end
16
26
  end
@@ -0,0 +1,16 @@
1
+ module Metaractor
2
+ module Delegation
3
+ def respond_to_missing?(name, include_private = false)
4
+ return false if name == :marshal_dump || name == :_dump
5
+ context.has_key?(name) || super
6
+ end
7
+
8
+ def method_missing(name, ...)
9
+ if context.has_key?(name)
10
+ context.public_send(name, ...)
11
+ else
12
+ super
13
+ end
14
+ end
15
+ end
16
+ end
@@ -1,3 +1,3 @@
1
1
  module Metaractor
2
- VERSION = "3.3.5"
2
+ VERSION = "3.4.1"
3
3
  end
data/lib/metaractor.rb CHANGED
@@ -8,6 +8,7 @@ require "metaractor/parameters"
8
8
  require "metaractor/run_with_context"
9
9
  require "metaractor/chain_failures"
10
10
  require "metaractor/failure_output"
11
+ require "metaractor/delegation"
11
12
  require "i18n"
12
13
  require "metaractor/namespace"
13
14
  require "metaractor/organizer"
@@ -45,7 +46,8 @@ module Metaractor
45
46
  {module: Metaractor::Parameters, method: :include},
46
47
  {module: Metaractor::RunWithContext, method: :include},
47
48
  {module: Metaractor::ChainFailures, method: :include},
48
- {module: Metaractor::Namespace, method: :include}
49
+ {module: Metaractor::Namespace, method: :include},
50
+ {module: Metaractor::Delegation, method: :include}
49
51
  ]
50
52
  end
51
53
 
data/metaractor.gemspec CHANGED
@@ -20,15 +20,15 @@ Gem::Specification.new do |spec|
20
20
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
21
  spec.require_paths = ["lib"]
22
22
 
23
- spec.add_runtime_dependency "interactor", "~> 3.1"
23
+ spec.add_runtime_dependency "interactor", "~> 3.2"
24
24
  spec.add_runtime_dependency "metaractor-sycamore", "~> 0.4", ">= 0.4.3"
25
25
  spec.add_runtime_dependency "i18n", "~> 1.14"
26
26
  spec.add_runtime_dependency "ostruct", "~> 0.6"
27
27
 
28
- spec.add_development_dependency "bundler", "~> 2"
28
+ spec.add_development_dependency "bundler", "~> 4"
29
29
  spec.add_development_dependency "rake", "~> 13.0"
30
30
  spec.add_development_dependency "rspec", "~> 3.13"
31
- spec.add_development_dependency "amazing_print", "~> 1.8"
31
+ spec.add_development_dependency "amazing_print", "~> 2.0"
32
32
  spec.add_development_dependency "debug", "~> 1.10"
33
33
  spec.add_development_dependency "activemodel", "~> 8"
34
34
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metaractor
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.3.5
4
+ version: 3.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Schlesinger
@@ -15,14 +15,14 @@ dependencies:
15
15
  requirements:
16
16
  - - "~>"
17
17
  - !ruby/object:Gem::Version
18
- version: '3.1'
18
+ version: '3.2'
19
19
  type: :runtime
20
20
  prerelease: false
21
21
  version_requirements: !ruby/object:Gem::Requirement
22
22
  requirements:
23
23
  - - "~>"
24
24
  - !ruby/object:Gem::Version
25
- version: '3.1'
25
+ version: '3.2'
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: metaractor-sycamore
28
28
  requirement: !ruby/object:Gem::Requirement
@@ -77,14 +77,14 @@ dependencies:
77
77
  requirements:
78
78
  - - "~>"
79
79
  - !ruby/object:Gem::Version
80
- version: '2'
80
+ version: '4'
81
81
  type: :development
82
82
  prerelease: false
83
83
  version_requirements: !ruby/object:Gem::Requirement
84
84
  requirements:
85
85
  - - "~>"
86
86
  - !ruby/object:Gem::Version
87
- version: '2'
87
+ version: '4'
88
88
  - !ruby/object:Gem::Dependency
89
89
  name: rake
90
90
  requirement: !ruby/object:Gem::Requirement
@@ -119,14 +119,14 @@ dependencies:
119
119
  requirements:
120
120
  - - "~>"
121
121
  - !ruby/object:Gem::Version
122
- version: '1.8'
122
+ version: '2.0'
123
123
  type: :development
124
124
  prerelease: false
125
125
  version_requirements: !ruby/object:Gem::Requirement
126
126
  requirements:
127
127
  - - "~>"
128
128
  - !ruby/object:Gem::Version
129
- version: '1.8'
129
+ version: '2.0'
130
130
  - !ruby/object:Gem::Dependency
131
131
  name: debug
132
132
  requirement: !ruby/object:Gem::Requirement
@@ -174,6 +174,7 @@ files:
174
174
  - lib/metaractor/chain_failures.rb
175
175
  - lib/metaractor/context_errors.rb
176
176
  - lib/metaractor/context_methods.rb
177
+ - lib/metaractor/delegation.rb
177
178
  - lib/metaractor/errors.rb
178
179
  - lib/metaractor/failure_output.rb
179
180
  - lib/metaractor/handle_errors.rb
@@ -203,7 +204,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
203
204
  - !ruby/object:Gem::Version
204
205
  version: '0'
205
206
  requirements: []
206
- rubygems_version: 3.6.7
207
+ rubygems_version: 4.0.10
207
208
  specification_version: 4
208
209
  summary: Adds parameter validation and error control to interactor
209
210
  test_files: []