metaractor 3.3.0 → 3.3.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
- require 'sycamore'
2
- require 'forwardable'
1
+ require "sycamore"
2
+ require "forwardable"
3
3
  module Metaractor
4
4
  class Errors
5
5
  extend Forwardable
@@ -21,15 +21,15 @@ module Metaractor
21
21
 
22
22
  names = object.class.i18n_parent_names
23
23
  until names.empty?
24
- defaults << ['errors', names.join('.'), 'parameters', path_elements.join('.'), @value.to_s].reject do |item|
25
- item.nil? || item == ''
26
- end.join('.').to_sym
24
+ defaults << ["errors", names.join("."), "parameters", path_elements.join("."), @value.to_s].reject do |item|
25
+ item.nil? || item == ""
26
+ end.join(".").to_sym
27
27
  names.pop
28
28
  end
29
29
  end
30
30
 
31
31
  unless path_elements.empty?
32
- defaults << :"errors.parameters.#{path_elements.join('.')}.#{@value}"
32
+ defaults << :"errors.parameters.#{path_elements.join(".")}.#{@value}"
33
33
  end
34
34
  defaults << :"errors.parameters.#{@value}"
35
35
 
@@ -41,18 +41,18 @@ module Metaractor
41
41
  parameter: path_elements.last
42
42
  )
43
43
  else
44
- "#{path_elements.join('.')} #{@value}".lstrip
44
+ "#{path_elements.join(".")} #{@value}".lstrip
45
45
  end
46
46
  end
47
47
 
48
48
  def ==(other)
49
- if other.is_a?(self.class)
50
- @value == other.value
49
+ @value == if other.is_a?(self.class)
50
+ other.value
51
51
  else
52
- @value == other
52
+ other
53
53
  end
54
54
  end
55
- alias eql? ==
55
+ alias_method :eql?, :==
56
56
 
57
57
  def hash
58
58
  @value.hash
@@ -73,10 +73,10 @@ module Metaractor
73
73
  trees = []
74
74
  [error, errors].each do |h|
75
75
  tree = nil
76
- if h.is_a? Metaractor::Errors
77
- tree = Sycamore::Tree.from(h.instance_variable_get(:@tree))
76
+ tree = if h.is_a? Metaractor::Errors
77
+ Sycamore::Tree.from(h.instance_variable_get(:@tree))
78
78
  else
79
- tree = Sycamore::Tree.from(normalize_error_hash(h))
79
+ Sycamore::Tree.from(normalize_error_hash(h))
80
80
  end
81
81
 
82
82
  unless tree.empty?
@@ -112,7 +112,7 @@ module Metaractor
112
112
 
113
113
  messages
114
114
  end
115
- alias to_a full_messages
115
+ alias_method :to_a, :full_messages
116
116
 
117
117
  def full_messages_for(*path)
118
118
  child_tree = @tree.fetch_path(path)
@@ -133,19 +133,17 @@ module Metaractor
133
133
  unwrapped_tree(result).to_h
134
134
  end
135
135
  end
136
- alias [] dig
136
+ alias_method :[], :dig
137
137
 
138
138
  def include?(*elements)
139
139
  if elements.size == 1 &&
140
140
  elements.first.is_a?(Hash)
141
141
  unwrapped_tree.include?(*elements)
142
+ elsif elements.all? { |e| e.is_a? String }
143
+ full_messages.include?(*elements)
142
144
  else
143
- if elements.all? {|e| e.is_a? String }
144
- full_messages.include?(*elements)
145
- else
146
- elements.all? do |element|
147
- @tree.include_path?(element)
148
- end
145
+ elements.all? do |element|
146
+ @tree.include_path?(element)
149
147
  end
150
148
  end
151
149
  end
@@ -169,12 +167,12 @@ module Metaractor
169
167
  @tree.to_h
170
168
  end
171
169
  end
172
- alias to_hash to_h
170
+ alias_method :to_hash, :to_h
173
171
 
174
172
  def inspect
175
173
  str = "<##{self.class.name}: "
176
174
 
177
- if !self.empty?
175
+ if !empty?
178
176
  str << "Errors:\n"
179
177
  str << Metaractor.format_hash(to_h(unwrap: false))
180
178
  str << "\n"
@@ -1,7 +1,7 @@
1
1
  module Metaractor
2
2
  module FailureOutput
3
3
  def to_s
4
- str = ''
4
+ str = ""
5
5
 
6
6
  if !context.errors.empty?
7
7
  str << "Errors:\n"
@@ -19,7 +19,7 @@ module Metaractor
19
19
  end
20
20
 
21
21
  str << "Context:\n"
22
- str << Metaractor.format_hash(context.to_h.reject{|k,_| k == :errors})
22
+ str << Metaractor.format_hash(context.to_h.except(:errors))
23
23
  str
24
24
  end
25
25
  end
@@ -1,5 +1,6 @@
1
1
  module Metaractor
2
2
  class Error < StandardError; end
3
+
3
4
  class InvalidError < Error; end
4
5
 
5
6
  module HandleErrors
@@ -11,7 +11,7 @@ module Metaractor
11
11
  if defined?(@parent_name)
12
12
  @parent_name
13
13
  else
14
- parent_name = name =~ /::[^:]+\z/ ? -$` : nil
14
+ parent_name = (name =~ /::[^:]+\z/) ? -$` : nil
15
15
  @parent_name = parent_name unless frozen?
16
16
  parent_name
17
17
  end
@@ -26,7 +26,7 @@ module Metaractor
26
26
  end
27
27
 
28
28
  def i18n_parent_names
29
- module_parent_names.map {|name| underscore_module_name(name).to_sym }
29
+ module_parent_names.map { |name| underscore_module_name(name).to_sym }
30
30
  end
31
31
 
32
32
  private
@@ -40,7 +40,6 @@ module Metaractor
40
40
  word.downcase!
41
41
  word
42
42
  end
43
-
44
43
  end
45
44
  end
46
45
  end
@@ -0,0 +1,10 @@
1
+ module Metaractor
2
+ module Organizer
3
+ def self.included(base)
4
+ base.class_eval do
5
+ include Metaractor
6
+ include Interactor::Organizer
7
+ end
8
+ end
9
+ end
10
+ end
@@ -31,7 +31,7 @@ module Metaractor
31
31
  def <=>(other)
32
32
  return nil unless other.instance_of? self.class
33
33
  return nil if name == other.name && options != other.options
34
- self.name.to_s <=> other.name.to_s
34
+ name.to_s <=> other.name.to_s
35
35
  end
36
36
 
37
37
  def [](key)
@@ -59,16 +59,17 @@ module Metaractor
59
59
  end
60
60
 
61
61
  protected
62
+
62
63
  attr_reader :options
63
64
  end
64
65
 
65
66
  module ClassMethods
66
67
  def parameter(name, **options)
67
- if param = self.parameter_hash[name.to_sym]
68
+ if (param = parameter_hash[name.to_sym])
68
69
  param.merge!(**options)
69
70
  else
70
71
  Parameter.new(name, **options).tap do |parameter|
71
- self.parameter_hash[parameter.name] = parameter
72
+ parameter_hash[parameter.name] = parameter
72
73
  end
73
74
  end
74
75
  end
@@ -90,7 +91,7 @@ module Metaractor
90
91
  def required(*params, **options)
91
92
  if params.empty?
92
93
  tree = options
93
- self.requirement_trees << tree
94
+ requirement_trees << tree
94
95
  parameters(*parameters_in_tree(tree), required: tree)
95
96
  else
96
97
  parameters(*params, required: true, **options)
@@ -103,7 +104,7 @@ module Metaractor
103
104
 
104
105
  def validate_parameters(*hooks, &block)
105
106
  hooks << block if block
106
- hooks.each {|hook| validate_hooks.push(hook) }
107
+ hooks.each { |hook| validate_hooks.push(hook) }
107
108
  end
108
109
 
109
110
  def validate_hooks
@@ -112,7 +113,7 @@ module Metaractor
112
113
 
113
114
  def parameters_in_tree(tree)
114
115
  if tree.respond_to?(:to_h)
115
- tree.to_h.values.first.to_a.flat_map {|t| parameters_in_tree(t)}
116
+ tree.to_h.values.first.to_a.flat_map { |t| parameters_in_tree(t) }
116
117
  else
117
118
  [tree]
118
119
  end
@@ -128,7 +129,7 @@ module Metaractor
128
129
  end
129
130
 
130
131
  def requirement_trees=(trees)
131
- self.class.requirement_trees=(trees)
132
+ self.class.requirement_trees = (trees)
132
133
  end
133
134
 
134
135
  def remove_blank_values
@@ -156,12 +157,14 @@ module Metaractor
156
157
  end
157
158
 
158
159
  def _parameter_default(name)
159
- default = self.parameters[name][:default]
160
+ default = parameters[name][:default]
160
161
 
161
- case
162
- when default.respond_to?(:call) then instance_exec(&default)
163
- when default.respond_to?(:dup) then default.dup
164
- else default
162
+ if default.respond_to?(:call)
163
+ instance_exec(&default)
164
+ elsif default.respond_to?(:dup)
165
+ default.dup
166
+ else
167
+ default
165
168
  end
166
169
  end
167
170
 
@@ -169,7 +172,7 @@ module Metaractor
169
172
  parameters.each do |name, parameter|
170
173
  next unless parameter[:type]
171
174
 
172
- if context.has_key?(name) && context[name] != nil
175
+ if context.has_key?(name) && !context[name].nil?
173
176
  callable = parameter[:type]
174
177
 
175
178
  if callable.is_a?(Symbol)
@@ -206,7 +209,7 @@ module Metaractor
206
209
  valid, message = parameter_valid? param
207
210
 
208
211
  if !valid
209
- if message_override != nil
212
+ if !message_override.nil?
210
213
  add_parameter_error(param: param, message: message_override)
211
214
  else
212
215
  add_parameter_error(message: "Required parameters: #{message}")
@@ -234,16 +237,16 @@ module Metaractor
234
237
 
235
238
  case operator
236
239
  when :or
237
- return valids.any?, "(#{messages.join(' or ')})"
240
+ [valids.any?, "(#{messages.join(" or ")})"]
238
241
  when :xor
239
- return valids.one?, "(#{messages.join(' xor ')})"
242
+ [valids.one?, "(#{messages.join(" xor ")})"]
240
243
  when :and
241
- return valids.all?, "(#{messages.join(' and ')})"
244
+ [valids.all?, "(#{messages.join(" and ")})"]
242
245
  else
243
246
  raise "invalid required parameter #{param.inspect}"
244
247
  end
245
248
  else
246
- return context[param] != nil, param.to_s
249
+ [!context[param].nil?, param.to_s]
247
250
  end
248
251
  end
249
252
 
@@ -256,7 +259,7 @@ module Metaractor
256
259
  run_hooks(self.class.validate_hooks)
257
260
  end
258
261
 
259
- def add_parameter_error(param: nil, message:)
262
+ def add_parameter_error(message:, param: nil)
260
263
  add_error(
261
264
  message: "#{param} #{message}".lstrip
262
265
  )
@@ -1,5 +1,5 @@
1
- require 'metaractor'
2
- require 'forwardable'
1
+ require "metaractor"
2
+ require "forwardable"
3
3
 
4
4
  module Metaractor
5
5
  module Spec
@@ -13,15 +13,19 @@ module Metaractor
13
13
  result.add_errors(messages: error_messages)
14
14
  result.add_errors(errors: errors)
15
15
 
16
- if (valid != nil && !valid) || (invalid != nil && invalid)
16
+ if (!valid.nil? && !valid) || (!invalid.nil? && invalid)
17
17
  result.invalidate!
18
18
  end
19
19
 
20
20
  if !result.errors.empty? ||
21
21
  result.invalid? ||
22
- (success != nil && !success) ||
23
- (failure != nil && failure)
24
- result.fail! rescue Interactor::Failure
22
+ (!success.nil? && !success) ||
23
+ (!failure.nil? && failure)
24
+ begin
25
+ result.fail!
26
+ rescue
27
+ Interactor::Failure
28
+ end
25
29
  end
26
30
 
27
31
  result
@@ -69,6 +73,5 @@ module Metaractor
69
73
  end
70
74
  end
71
75
  end
72
-
73
76
  end
74
77
  end
@@ -1,3 +1,3 @@
1
1
  module Metaractor
2
- VERSION = "3.3.0"
2
+ VERSION = "3.3.3"
3
3
  end
data/lib/metaractor.rb CHANGED
@@ -1,17 +1,18 @@
1
- require 'metaractor/version'
2
- require 'interactor'
3
- require 'metaractor/errors'
4
- require 'metaractor/handle_errors'
5
- require 'metaractor/context_errors'
6
- require 'metaractor/parameters'
7
- require 'metaractor/run_with_context'
8
- require 'metaractor/context_validity'
9
- require 'metaractor/chain_failures'
10
- require 'metaractor/fail_from_context'
11
- require 'metaractor/context_has_key'
12
- require 'metaractor/failure_output'
13
- require 'i18n'
14
- require 'metaractor/namespace'
1
+ require "metaractor/version"
2
+ require "interactor"
3
+ require "metaractor/errors"
4
+ require "metaractor/handle_errors"
5
+ require "metaractor/context_errors"
6
+ require "metaractor/parameters"
7
+ require "metaractor/run_with_context"
8
+ require "metaractor/context_validity"
9
+ require "metaractor/chain_failures"
10
+ require "metaractor/fail_from_context"
11
+ require "metaractor/context_has_key"
12
+ require "metaractor/failure_output"
13
+ require "i18n"
14
+ require "metaractor/namespace"
15
+ require "metaractor/organizer"
15
16
 
16
17
  module Metaractor
17
18
  def self.included(base)
@@ -42,20 +43,20 @@ module Metaractor
42
43
 
43
44
  def self.default_modules
44
45
  [
45
- { module: Metaractor::HandleErrors, method: :include },
46
- { module: Metaractor::Parameters, method: :include },
47
- { module: Metaractor::RunWithContext, method: :include },
48
- { module: Metaractor::ChainFailures, method: :include },
49
- { module: Metaractor::Namespace, method: :include }
46
+ {module: Metaractor::HandleErrors, method: :include},
47
+ {module: Metaractor::Parameters, method: :include},
48
+ {module: Metaractor::RunWithContext, method: :include},
49
+ {module: Metaractor::ChainFailures, method: :include},
50
+ {module: Metaractor::Namespace, method: :include}
50
51
  ]
51
52
  end
52
53
 
53
54
  def self.include_module(mod)
54
- modules << { module: mod, method: :include }
55
+ modules << {module: mod, method: :include}
55
56
  end
56
57
 
57
58
  def self.prepend_module(mod)
58
- modules << { module: mod, method: :prepend }
59
+ modules << {module: mod, method: :prepend}
59
60
  end
60
61
 
61
62
  def self.format_hash(hash)
@@ -67,7 +68,7 @@ module Metaractor
67
68
  end
68
69
 
69
70
  def self.default_hash_formatter
70
- ->(hash){ hash.inspect }
71
+ ->(hash) { hash.inspect }
71
72
  end
72
73
 
73
74
  def self.hash_formatter
data/metaractor.gemspec CHANGED
@@ -1,34 +1,33 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
1
+ lib = File.expand_path("../lib", __FILE__)
3
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'metaractor/version'
3
+ require "metaractor/version"
5
4
 
6
5
  Gem::Specification.new do |spec|
7
- spec.name = 'metaractor'
8
- spec.version = Metaractor::VERSION
9
- spec.license = 'Apache-2.0'
10
- spec.authors = ['Ryan Schlesinger']
11
- spec.email = ['ryan@outstand.com']
6
+ spec.name = "metaractor"
7
+ spec.version = Metaractor::VERSION
8
+ spec.license = "Apache-2.0"
9
+ spec.authors = ["Ryan Schlesinger"]
10
+ spec.email = ["ryan@ryanschlesinger.com"]
12
11
 
13
- spec.summary = %q{Adds parameter validation and error control to interactor}
14
- spec.metadata = {
15
- "homepage_uri" => "https://github.com/outstand/metaractor",
16
- "source_code_uri" => "https://github.com/outstand/metaractor"
12
+ spec.summary = "Adds parameter validation and error control to interactor"
13
+ spec.metadata = {
14
+ "homepage_uri" => "https://github.com/metaractor/metaractor",
15
+ "source_code_uri" => "https://github.com/metaractor/metaractor"
17
16
  }
18
17
 
19
- spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
- spec.bindir = 'exe'
21
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
- spec.require_paths = ['lib']
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
23
22
 
24
- spec.add_runtime_dependency 'interactor', '~> 3.1'
25
- spec.add_runtime_dependency 'outstand-sycamore', '0.4.0'
26
- spec.add_runtime_dependency 'i18n', '~> 1.8'
23
+ spec.add_runtime_dependency "interactor", "~> 3.1"
24
+ spec.add_runtime_dependency "metaractor-sycamore", "~> 0.4", ">= 0.4.3"
25
+ spec.add_runtime_dependency "i18n", "~> 1.8"
27
26
 
28
- spec.add_development_dependency 'bundler', '~> 2'
29
- spec.add_development_dependency 'rake', '~> 13.0'
30
- spec.add_development_dependency 'rspec', '~> 3.9'
31
- spec.add_development_dependency 'awesome_print', '~> 1.8'
32
- spec.add_development_dependency 'pry-byebug', '~> 3.9'
33
- spec.add_development_dependency 'activemodel', '~> 6.1'
27
+ spec.add_development_dependency "bundler", "~> 2"
28
+ spec.add_development_dependency "rake", "~> 13.0"
29
+ spec.add_development_dependency "rspec", "~> 3.9"
30
+ spec.add_development_dependency "awesome_print", "~> 1.8"
31
+ spec.add_development_dependency "pry-byebug", "~> 3.9"
32
+ spec.add_development_dependency "activemodel", "~> 6.1"
34
33
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metaractor
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.3.0
4
+ version: 3.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Schlesinger
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-08-26 00:00:00.000000000 Z
11
+ date: 2023-07-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: interactor
@@ -25,19 +25,25 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '3.1'
27
27
  - !ruby/object:Gem::Dependency
28
- name: outstand-sycamore
28
+ name: metaractor-sycamore
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '='
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.4'
34
+ - - ">="
32
35
  - !ruby/object:Gem::Version
33
- version: 0.4.0
36
+ version: 0.4.3
34
37
  type: :runtime
35
38
  prerelease: false
36
39
  version_requirements: !ruby/object:Gem::Requirement
37
40
  requirements:
38
- - - '='
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: '0.4'
44
+ - - ">="
39
45
  - !ruby/object:Gem::Version
40
- version: 0.4.0
46
+ version: 0.4.3
41
47
  - !ruby/object:Gem::Dependency
42
48
  name: i18n
43
49
  requirement: !ruby/object:Gem::Requirement
@@ -136,25 +142,24 @@ dependencies:
136
142
  - - "~>"
137
143
  - !ruby/object:Gem::Version
138
144
  version: '6.1'
139
- description:
145
+ description:
140
146
  email:
141
- - ryan@outstand.com
147
+ - ryan@ryanschlesinger.com
142
148
  executables: []
143
149
  extensions: []
144
150
  extra_rdoc_files: []
145
151
  files:
146
- - ".buildkite/pipeline.yml"
152
+ - ".envrc"
153
+ - ".github/workflows/specs.yml"
147
154
  - ".gitignore"
148
155
  - ".rspec"
149
- - Deskfile
150
- - Dockerfile
156
+ - ".ruby-version"
151
157
  - Gemfile
152
158
  - LICENSE
153
159
  - README.md
154
160
  - Rakefile
155
- - brew-shim
156
- - compose.yml
157
- - docker-entrypoint.sh
161
+ - flake.lock
162
+ - flake.nix
158
163
  - lib/metaractor.rb
159
164
  - lib/metaractor/chain_failures.rb
160
165
  - lib/metaractor/context_errors.rb
@@ -165,18 +170,19 @@ files:
165
170
  - lib/metaractor/failure_output.rb
166
171
  - lib/metaractor/handle_errors.rb
167
172
  - lib/metaractor/namespace.rb
173
+ - lib/metaractor/organizer.rb
168
174
  - lib/metaractor/parameters.rb
169
175
  - lib/metaractor/run_with_context.rb
170
176
  - lib/metaractor/spec.rb
171
177
  - lib/metaractor/version.rb
172
178
  - metaractor.gemspec
173
- homepage:
179
+ homepage:
174
180
  licenses:
175
181
  - Apache-2.0
176
182
  metadata:
177
- homepage_uri: https://github.com/outstand/metaractor
178
- source_code_uri: https://github.com/outstand/metaractor
179
- post_install_message:
183
+ homepage_uri: https://github.com/metaractor/metaractor
184
+ source_code_uri: https://github.com/metaractor/metaractor
185
+ post_install_message:
180
186
  rdoc_options: []
181
187
  require_paths:
182
188
  - lib
@@ -191,8 +197,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
191
197
  - !ruby/object:Gem::Version
192
198
  version: '0'
193
199
  requirements: []
194
- rubygems_version: 3.3.7
195
- signing_key:
200
+ rubygems_version: 3.4.10
201
+ signing_key:
196
202
  specification_version: 4
197
203
  summary: Adds parameter validation and error control to interactor
198
204
  test_files: []