gl_command 1.1.4 → 1.3.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: 2b963d760cbf796923d8ade7638d344076b2c7d08f9a9bfcd42a2153fea79201
4
- data.tar.gz: b468cf7458eab4c4e9c145a82d59010d145a7d06bd5bb8b81b3fb9e6e0766f35
3
+ metadata.gz: d64529ae90558047a1a67fb4641fcbc6807f4bac0ef8b503945d2981dac33f80
4
+ data.tar.gz: 541c75d629f1ba4971ab7d6fd37ac4a1fdfee2f19cb6256477d4857408fb64ce
5
5
  SHA512:
6
- metadata.gz: 9ced69bc84bbe0d55bfb5ea324334165fdd3acdc4a06b45252ff9397cb3f70193d8cbcff7a75dbc3b1a852fcc00f223d84add9294047ddf731ea5e121fb70db4
7
- data.tar.gz: 30ab133f7d7d10ccd0b3d7e5f29b6121c064c8518f6e528253f96b5713bf04308051600acd51948004583541083a267e1bf0718c0da7266c800241c3f7d71ad8
6
+ metadata.gz: 7767272a19c79ed50bc21bcd5a94a04f8f16c26e509bdfc3b86937e8204dabf2d053ea2fc85919acc56de3a770fd35ff88490b2d101fdcf6e6a89a714c55176f
7
+ data.tar.gz: f4fd2347bedf5ccbd94c09e230bf760d16d0ec1f19a4ecada73416d7a4b0da52a7f79759f074c4a38f98cd783ce45a07dd84c46dd1f13b87685cfdec8d0e9c9e
data/README.md CHANGED
@@ -215,6 +215,24 @@ end
215
215
  ```
216
216
 
217
217
 
218
+ ## Publishing the gem to Rubygems
219
+
220
+ 1. As per our code agreements, all code changes to this gem are required to be made via pull request with final approval from at least one Give Lively engineer.
221
+
222
+ 2. When creating a pull request, ensure that your code changes include an update to the gem's [version number](https://github.com/givelively/gl_command/blob/main/lib/gl_command/version.rb) using [semantic versioning](https://semver.org/)
223
+
224
+ 3. After getting approval, merge your changes to `main`.
225
+
226
+ 4. Once your CI build finishes successfully, pull the latest version of `main` locally.
227
+
228
+ 5. Run the command `gem build`. This bundles the relevant files from the gem and prepares it to be published to [rubygems.org](https://rubygems.org/).
229
+
230
+ 6. This will create a new file locally that looks like `gl_command-<new_gem_version_number>.gem`.
231
+
232
+ 7. Run `gem push gl_command-<new_gem_version_number>.gem` to publish the new version
233
+
234
+ **NOTE: only the gem owners listed on rubygems can publish new versions**
235
+
218
236
  ---
219
237
 
220
238
  This library is influenced by [interactors](https://github.com/collectiveidea/interactor) and inspired by the [Command Pattern](https://en.wikipedia.org/wiki/Command_pattern).
@@ -6,6 +6,7 @@ require 'gl_command/validatable'
6
6
 
7
7
  module GLCommand
8
8
  class Callable
9
+ include GLCommand::Validatable
9
10
  ALWAYS_RAISE_ERRORS = ENV['GL_COMMAND_ALWAYS_RAISE'] == 'true'
10
11
  DEFAULT_OPTS = { raise_errors: false, skip_unknown_parameters: true, in_chain: false }.freeze
11
12
  RESERVED_WORDS = (DEFAULT_OPTS.keys + GLCommand::ChainableContext.reserved_words).sort.freeze
@@ -102,8 +103,6 @@ module GLCommand
102
103
  end
103
104
  end
104
105
 
105
- include GLCommand::Validatable
106
-
107
106
  attr_reader :context
108
107
 
109
108
  def initialize(context = nil)
@@ -203,6 +202,7 @@ module GLCommand
203
202
 
204
203
  chain_rollback if self.class.chain? # defined in GLCommand::Chainable
205
204
  rollback
205
+ instrument_command(:after_rollback)
206
206
  end
207
207
 
208
208
  # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
@@ -41,7 +41,7 @@ module GLCommand
41
41
 
42
42
  commands.map do |command|
43
43
  cargs = context.chain_arguments_and_returns.slice(*command.arguments)
44
- .merge(context.opts_hash).merge(in_chain: true)
44
+ .merge(context.opts_hash).merge(in_chain: context)
45
45
 
46
46
  # using _result to make sure it doesn't cause naming conflicts with other uses of result
47
47
  _result = command.call(**cargs)
@@ -22,7 +22,7 @@ module GLCommand
22
22
  { raise_errors: raise_errors? }
23
23
  end
24
24
 
25
- attr_reader :klass, :error
25
+ attr_reader :klass, :error, :in_chain
26
26
  attr_writer :full_error_message
27
27
 
28
28
  # If someone calls #errors, they expect to get the errors! Include the non-validation error, if it exists
@@ -35,10 +35,6 @@ module GLCommand
35
35
  false
36
36
  end
37
37
 
38
- def in_chain?
39
- @in_chain
40
- end
41
-
42
38
  def returns
43
39
  @klass.returns.index_with { |rattr| send(rattr) }
44
40
  end
@@ -1,5 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GLCommand
2
4
  class ContextInspect
5
+ PERMITTED_OUTPUTS = %i[string hash].freeze
6
+
3
7
  class << self
4
8
  def error(error_obj)
5
9
  return '' if error_obj.blank?
@@ -7,35 +11,50 @@ module GLCommand
7
11
  error_obj.is_a?(Array) ? error_obj.uniq.join(', ') : error_obj.to_s
8
12
  end
9
13
 
10
- def hash_params(hash)
11
- hash.map do |key, value|
12
- value_s =
13
- if value.nil?
14
- 'nil'
15
- elsif value.respond_to?(:to_sql)
16
- object_param_as_sql(value)
17
- elsif value.respond_to?(:uuid)
18
- object_param_with_id(value, :uuid)
19
- elsif value.respond_to?(:id)
20
- object_param_with_id(value, :id)
21
- else
22
- value
23
- end
24
- "#{key}: #{value_s}"
25
- end.join(', ')
14
+ def hash_params(hash, output: :string)
15
+ unless PERMITTED_OUTPUTS.include?(output)
16
+ raise "Unknown output type: #{output}, must be one of #{PERMITTED_OUTPUTS}"
17
+ end
18
+
19
+ result = hash.map { |key, value| output_for(key:, value:, output:) }
20
+ output == :string ? result.join(', ') : result.to_h
26
21
  end
27
22
 
28
23
  private
29
24
 
25
+ def output_for(key:, value:, output:)
26
+ value_s = if value.nil?
27
+ 'nil'
28
+ elsif value.respond_to?(:to_sql)
29
+ object_param_as_sql(value, output:)
30
+ elsif value.respond_to?(:id)
31
+ object_param_with_id(value, :id, output:)
32
+ elsif value.respond_to?(:uuid)
33
+ object_param_with_id(value, :uuid, output:)
34
+ else
35
+ value
36
+ end
37
+
38
+ output == :string ? "#{key}: #{value_s}" : [key, value_s]
39
+ end
40
+
30
41
  # Active record objects can be really big - rather than rendering the whole object, just show the ID
31
- def object_param_with_id(obj, key)
42
+ def object_param_with_id(obj, key, output:)
32
43
  obj_id = obj.send(key)
33
- id_value = obj_id.is_a?(Integer) ? obj_id : "\"#{obj_id}\""
34
- "#<#{obj.class.name} #{key}=#{id_value}>"
44
+ if output == :string
45
+ id_value = obj_id.is_a?(Integer) ? obj_id : "\"#{obj_id}\""
46
+ "#<#{obj.class.name} #{key}=#{id_value}>"
47
+ else
48
+ obj_id
49
+ end
35
50
  end
36
51
 
37
- def object_param_as_sql(obj)
38
- "#<#{obj.class.name} sql=\"#{obj.to_sql}\">"
52
+ def object_param_as_sql(obj, output:)
53
+ if output == :string
54
+ "#<#{obj.class.name} sql=\"#{obj.to_sql}\">"
55
+ else
56
+ obj.to_sql
57
+ end
39
58
  end
40
59
  end
41
60
  end
@@ -1,3 +1,3 @@
1
1
  module GLCommand
2
- VERSION = '1.1.4'.freeze
2
+ VERSION = '1.3.0'.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.4
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Give Lively
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-03-18 00:00:00.000000000 Z
11
+ date: 2025-05-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord