gl_command 1.1.0 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b0e88d785a51d72653d8691d86236df3791dadaa84b46a548895e045509e1428
4
- data.tar.gz: 582d39e38c74b40ac251dd91071fe602a8860f6ecda15a7ece244e21541352d1
3
+ metadata.gz: 72008b456b363007dc64f9701fb5e9241248527475df1900f8026d1f4fa1d63f
4
+ data.tar.gz: 368147f6acc900a820233283538ac7bcdb10c75ce0af3803076da9d87f919151
5
5
  SHA512:
6
- metadata.gz: e8758b174cdf1f76a4b35d466876a8fbaa848f2546628ea777e7db594a1221a3ca9db2c31f0be77a6d58c62a739f62294e23e63050ed76bd48ca85fcbbfcdb0b
7
- data.tar.gz: ed20e53cb69da3c59f2919e7f89c505d2470a9fa44138fdd9334c12c6bebcb1c89dd34a9598b9f33a0c0a29ea875fefa7f10c89e16a2d0cfe07fb1bcb133c392
6
+ metadata.gz: 6ec728362eb5515db172717065350598ab7c1c636519a62e61c7c6e18b4a3cae724eff2feaa5845167f05f4c45d43d008a9df664bdb2c04c4ed427d1884765c3
7
+ data.tar.gz: 4314e4fb784d4c275585ba3952024189c76504a53a5d372b7239c4616ea08c5728237fd0bb6800acf23068189059be2de777e6b1d95063e6a68379a8eef5dc58
@@ -35,9 +35,13 @@ module GLCommand
35
35
  call(*posargs, **args.merge(raise_errors: true))
36
36
  end
37
37
 
38
- def build_context(raise_errors: false, skip_unknown_parameters: false,
38
+ # error can be passed to build context, useful for stubbing in tests
39
+ def build_context(raise_errors: false, skip_unknown_parameters: false, error: nil,
39
40
  **arguments_and_returns)
40
- context_class.new(self, raise_errors:, skip_unknown_parameters:, **arguments_and_returns)
41
+ new_context = context_class.new(self, raise_errors:, skip_unknown_parameters:,
42
+ **arguments_and_returns)
43
+ new_context.error = error if error.present?
44
+ new_context
41
45
  end
42
46
 
43
47
  def requires(*attributes, **strong_attributes)
@@ -25,7 +25,11 @@ module GLCommand
25
25
  attr_reader :klass, :error
26
26
  attr_writer :full_error_message
27
27
 
28
- delegate :errors, to: :@callable, allow_nil: true
28
+ # If someone calls #errors, they expect to get the errors! Include the non-validation error, if it exists
29
+ def errors
30
+ current_errors&.add(:base, "Command Error: #{full_error_message}") if add_command_error?
31
+ current_errors
32
+ end
29
33
 
30
34
  def chain?
31
35
  false
@@ -48,7 +52,7 @@ module GLCommand
48
52
  end
49
53
 
50
54
  def failure?
51
- @failure || errors.present? || @full_error_message.present? || false
55
+ @failure || current_errors.present? || @full_error_message.present? || false
52
56
  end
53
57
 
54
58
  def success?
@@ -103,7 +107,7 @@ module GLCommand
103
107
  passed_error.is_a?(ActiveRecord::RecordInvalid) && defined?(passed_error.record.errors)
104
108
  # Return a new error if it's an error (rather than the class)
105
109
  passed_error.is_a?(Class) ? passed_error.new(@full_error_message) : passed_error
106
- elsif errors.present? # check for validation errors
110
+ elsif current_errors.present? # check for validation errors
107
111
  # Assign ActiveRecord::RecordInvalid if validatable error
108
112
  ActiveRecord::RecordInvalid.new(@callable)
109
113
  else
@@ -123,6 +127,20 @@ module GLCommand
123
127
 
124
128
  private
125
129
 
130
+ def current_errors
131
+ @callable&.errors
132
+ end
133
+
134
+ def add_command_error?
135
+ return false if @error.blank?
136
+
137
+ return true if current_errors.blank?
138
+
139
+ # Add command error unless the existing error is a validation error or there's already a command error
140
+ @error&.class != ActiveRecord::RecordInvalid &&
141
+ current_errors.full_messages.none? { |err| err.start_with?('Command Error: ') }
142
+ end
143
+
126
144
  def exception?(passed_error)
127
145
  passed_error.is_a?(Exception) ||
128
146
  (passed_error.respond_to?(:ancestors) && passed_error.ancestors.include?(Exception))
@@ -136,7 +154,7 @@ module GLCommand
136
154
  def merge_errors(new_errors)
137
155
  # When merging the errors, don't add duplicate errors
138
156
  new_errors.each do |new_error|
139
- errors.import(new_error) unless errors&.full_messages&.include?(new_error.full_message)
157
+ current_errors.import(new_error) unless current_errors&.full_messages&.include?(new_error.full_message)
140
158
  end
141
159
  end
142
160
 
@@ -1,3 +1,3 @@
1
1
  module GLCommand
2
- VERSION = '1.1.0'.freeze
2
+ VERSION = '1.1.2'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gl_command
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Give Lively
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-08-28 00:00:00.000000000 Z
11
+ date: 2024-09-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord