lite-command 2.1.1 → 2.1.3
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 +4 -4
- data/.github/workflows/ci.yml +21 -0
- data/.rubocop.yml +1 -0
- data/CHANGELOG.md +8 -0
- data/Gemfile.lock +1 -8
- data/README.md +66 -47
- data/lib/lite/command/attribute.rb +10 -1
- data/lib/lite/command/base.rb +2 -2
- data/lib/lite/command/version.rb +1 -1
- data/lite-command.gemspec +0 -1
- metadata +3 -18
- data/.fasterer.yml +0 -19
- data/.travis.yml +0 -25
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7d33153b2a39998b16565b6a486c71ac29ce975b784b9e7f0a0c1c053712d703
|
4
|
+
data.tar.gz: 261fa88f0032d58d169d35e141bc76ca0770486b8fe0bc7fd65a3dbc5e269905
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2899e8d37519cb9a896f5573f230323141f24b99598f4a0674580fd795d51e92f2c7bbd32b875e6755779f6cfead9aacd81851ba2cfbaa8652b318c2849a924e
|
7
|
+
data.tar.gz: f4e9b2bb0eaec8320c0e18f2c75768479369ae8f2cec0b1fce0812b0814b0e4e2a442189fb207fa34031557839b42aa4580590d39b8c87faaa8ca41b17803787
|
@@ -0,0 +1,21 @@
|
|
1
|
+
name: CI
|
2
|
+
on:
|
3
|
+
pull_request:
|
4
|
+
branches: [ master ]
|
5
|
+
permissions:
|
6
|
+
contents: read
|
7
|
+
jobs:
|
8
|
+
test:
|
9
|
+
runs-on: ubuntu-latest
|
10
|
+
steps:
|
11
|
+
- name: Checkout
|
12
|
+
uses: actions/checkout@v4
|
13
|
+
- name: Install
|
14
|
+
uses: ruby/setup-ruby@v1
|
15
|
+
with:
|
16
|
+
ruby-version: '3.3.5'
|
17
|
+
bundler-cache: true
|
18
|
+
- name: RSpec
|
19
|
+
run: bundle exec rspec
|
20
|
+
- name: Rubocop
|
21
|
+
run: bundle exec rubocop --parallel
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
6
6
|
|
7
7
|
## [Unreleased]
|
8
8
|
|
9
|
+
## [2.1.3] - 2024-10-08
|
10
|
+
### Changed
|
11
|
+
- Move super in inheritance
|
12
|
+
|
13
|
+
## [2.1.2] - 2024-10-08
|
14
|
+
### Added
|
15
|
+
- Allow `filled` to pass `{ empty: false }` to check if value is empty
|
16
|
+
|
9
17
|
## [2.1.1] - 2024-10-06
|
10
18
|
### Added
|
11
19
|
- Added on_status hook to `execute!`
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
lite-command (2.1.
|
4
|
+
lite-command (2.1.3)
|
5
5
|
ostruct
|
6
6
|
|
7
7
|
GEM
|
@@ -45,8 +45,6 @@ GEM
|
|
45
45
|
diff-lcs (1.5.1)
|
46
46
|
drb (2.2.1)
|
47
47
|
erubi (1.13.0)
|
48
|
-
fasterer (0.11.0)
|
49
|
-
ruby_parser (>= 3.19.1)
|
50
48
|
generator_spec (0.10.0)
|
51
49
|
activesupport (>= 3.0.0)
|
52
50
|
railties (>= 3.0.0)
|
@@ -138,11 +136,7 @@ GEM
|
|
138
136
|
rubocop-rspec (3.1.0)
|
139
137
|
rubocop (~> 1.61)
|
140
138
|
ruby-progressbar (1.13.0)
|
141
|
-
ruby_parser (3.21.1)
|
142
|
-
racc (~> 1.5)
|
143
|
-
sexp_processor (~> 4.16)
|
144
139
|
securerandom (0.3.1)
|
145
|
-
sexp_processor (4.17.2)
|
146
140
|
sqlite3 (2.1.0)
|
147
141
|
mini_portile2 (~> 2.8.0)
|
148
142
|
stringio (3.1.1)
|
@@ -159,7 +153,6 @@ PLATFORMS
|
|
159
153
|
|
160
154
|
DEPENDENCIES
|
161
155
|
bundler
|
162
|
-
fasterer
|
163
156
|
generator_spec
|
164
157
|
lite-command!
|
165
158
|
rake
|
data/README.md
CHANGED
@@ -8,8 +8,8 @@ Lite::Command provides an API for building simple and complex command based serv
|
|
8
8
|
|
9
9
|
Add this line to your application's Gemfile:
|
10
10
|
|
11
|
-
> [!
|
12
|
-
> Gem versions
|
11
|
+
> [!WARNING]
|
12
|
+
> Gem versions `~> 2.0` are borked.
|
13
13
|
> Version `~> 2.1.0` is the suggested working version.
|
14
14
|
|
15
15
|
```ruby
|
@@ -48,7 +48,8 @@ Or install it yourself as:
|
|
48
48
|
|
49
49
|
## Setup
|
50
50
|
|
51
|
-
Defining a command is as simple as
|
51
|
+
Defining a command is as simple as inheriting the base class and
|
52
|
+
adding a `call` method to a command object (required).
|
52
53
|
|
53
54
|
```ruby
|
54
55
|
class CalculatePower < Lite::Command::Base
|
@@ -133,7 +134,7 @@ class CalculatePower < Lite::Command::Base
|
|
133
134
|
end
|
134
135
|
|
135
136
|
CalculatePower.call!(...)
|
136
|
-
#=> raises CalculatePower::
|
137
|
+
#=> raises CalculatePower::Failure
|
137
138
|
```
|
138
139
|
|
139
140
|
## Context
|
@@ -142,6 +143,9 @@ Accessing the call arguments can be done through its internal context.
|
|
142
143
|
It can be used as internal storage to be accessed by it self and any
|
143
144
|
of its children commands.
|
144
145
|
|
146
|
+
> [!NOTE]
|
147
|
+
> Attributes that do **NOT** exist on the context will return `nil`.
|
148
|
+
|
145
149
|
```ruby
|
146
150
|
class CalculatePower < Lite::Command::Base
|
147
151
|
|
@@ -152,8 +156,9 @@ class CalculatePower < Lite::Command::Base
|
|
152
156
|
|
153
157
|
end
|
154
158
|
|
155
|
-
|
156
|
-
|
159
|
+
cmd = CalculatePower.call(a: 2, b: 3)
|
160
|
+
cmd.context.result #=> 8
|
161
|
+
cmd.ctx.fake #=> nil
|
157
162
|
```
|
158
163
|
|
159
164
|
### Attributes
|
@@ -167,7 +172,7 @@ method which automatically delegates to `context`.
|
|
167
172
|
| `from` | Symbol, String | `:context` | The object containing the attribute. |
|
168
173
|
| `types` | Symbol, String, Array, Proc | | The allowed class types of the attribute value. |
|
169
174
|
| `required` | Symbol, String, Boolean, Proc | `false` | The attribute must be passed to the context or delegatable (no matter the value). |
|
170
|
-
| `filled` | Symbol, String, Boolean, Proc | `false` | The attribute value must be not be `nil`. |
|
175
|
+
| `filled` | Symbol, String, Boolean, Proc, Hash | `false` | The attribute value must be not be `nil`. Prevent empty values using `{ empty: false }` |
|
171
176
|
|
172
177
|
> [!NOTE]
|
173
178
|
> If optioned with some similar to `filled: true, types: [String, NilClass]`
|
@@ -180,7 +185,7 @@ class CalculatePower < Lite::Command::Base
|
|
180
185
|
|
181
186
|
attribute :a, :b
|
182
187
|
attribute :c, :d, from: :remote_storage, types: [Integer, Float]
|
183
|
-
attribute :x, :y, from: :local_storage, if: :signed_in?
|
188
|
+
attribute :x, :y, from: :local_storage, filled: { empty: false }, if: :signed_in?
|
184
189
|
|
185
190
|
def call
|
186
191
|
context.result =
|
@@ -202,20 +207,20 @@ class CalculatePower < Lite::Command::Base
|
|
202
207
|
end
|
203
208
|
|
204
209
|
# With valid options:
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
# With invalid options
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
210
|
+
rs = RemoteStorage.new(c: 2, d: 2, j: 99)
|
211
|
+
cmd = CalculatePower.call(a: 2, b: 2, remote_storage: rs)
|
212
|
+
cmd.status #=> "success"
|
213
|
+
cmd.context.result #=> 6
|
214
|
+
|
215
|
+
# With invalid options:
|
216
|
+
cmd = CalculatePower.call
|
217
|
+
cmd.status #=> "invalid"
|
218
|
+
cmd.reason #=> "Invalid context attributes"
|
219
|
+
cmd.metadata #=> {
|
220
|
+
#=> context: ["a is required", "remote_storage must be filled"],
|
221
|
+
#=> remote_storage: ["d type invalid"]
|
222
|
+
#=> local_storage: ["is not defined or an attribute"]
|
223
|
+
#=> }
|
219
224
|
```
|
220
225
|
|
221
226
|
## States
|
@@ -232,18 +237,16 @@ command.metadata #=> {
|
|
232
237
|
> States are automatically transitioned and should **NEVER** be altered manually.
|
233
238
|
|
234
239
|
```ruby
|
235
|
-
|
240
|
+
cmd = CalculatePower.call
|
241
|
+
cmd.state #=> "complete"
|
236
242
|
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
end
|
243
|
+
cmd.pending? #=> false
|
244
|
+
cmd.executing? #=> false
|
245
|
+
cmd.complete? #=> true
|
246
|
+
cmd.interrupted? #=> false
|
242
247
|
|
243
|
-
|
244
|
-
|
245
|
-
command.pending? #=> false
|
246
|
-
command.executed? #=> false
|
248
|
+
# `complete` or `interrupted`
|
249
|
+
cmd.executed?
|
247
250
|
```
|
248
251
|
|
249
252
|
## Statuses
|
@@ -285,14 +288,25 @@ class CalculatePower < Lite::Command::Base
|
|
285
288
|
|
286
289
|
end
|
287
290
|
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
291
|
+
cmd = CalculatePower.call(a: 1, b: 3)
|
292
|
+
cmd.status #=> "noop"
|
293
|
+
cmd.reason #=> "Anything to the power of 1 is 1"
|
294
|
+
cmd.metadata #=> { i18n: "some.key" }
|
295
|
+
|
296
|
+
cmd.success? #=> false
|
297
|
+
cmd.noop? #=> true
|
298
|
+
cmd.noop?("Other reason") #=> false
|
299
|
+
cmd.invalid? #=> false
|
300
|
+
cmd.failure? #=> false
|
301
|
+
cmd.error? #=> false
|
302
|
+
|
303
|
+
# `success` or `noop`
|
304
|
+
cmd.ok? #=> true
|
305
|
+
cmd.ok?("Other reason") #=> false
|
306
|
+
|
307
|
+
# NOT `success`
|
308
|
+
cmd.fault? #=> true
|
309
|
+
cmd.fault?("Other reason") #=> false
|
296
310
|
```
|
297
311
|
|
298
312
|
## Callbacks
|
@@ -423,7 +437,8 @@ that it gains automated indexing and the parents `cmd_id`.
|
|
423
437
|
class CalculatePower < Lite::Command::Base
|
424
438
|
|
425
439
|
def call
|
426
|
-
|
440
|
+
context.merge!(some_other: "required value")
|
441
|
+
CalculateSqrt.call(context)
|
427
442
|
end
|
428
443
|
|
429
444
|
end
|
@@ -434,7 +449,8 @@ end
|
|
434
449
|
Throwing faults allows you to bubble up child faults up to the parent.
|
435
450
|
Use it to create branches within your logic and create clean tracing
|
436
451
|
of your command results. You can use `throw!` as a catch-all or any
|
437
|
-
of the bang status method `failure!`.
|
452
|
+
of the bang status method `failure!`. Any `reason` and `metadata` will
|
453
|
+
be bubbled up from the original fault.
|
438
454
|
|
439
455
|
```ruby
|
440
456
|
class CalculatePower < Lite::Command::Base
|
@@ -443,10 +459,10 @@ class CalculatePower < Lite::Command::Base
|
|
443
459
|
command = CalculateSqrt.call(context.merge!(some_other: "required value"))
|
444
460
|
|
445
461
|
if command.noop?("Sqrt of 1 is 1")
|
446
|
-
# Manually throw
|
462
|
+
# Manually throw a specific fault
|
447
463
|
invalid!(command)
|
448
464
|
elsif command.fault?
|
449
|
-
# Automatically throws a matching fault
|
465
|
+
# Automatically throws a matching fault
|
450
466
|
throw!(command)
|
451
467
|
else
|
452
468
|
# Success, do nothing
|
@@ -467,6 +483,10 @@ This is useful for composing multiple steps into one call.
|
|
467
483
|
> so its no different than just passing the context forward. To change
|
468
484
|
> this behavior, just override the `ok?` method with you logic, eg: just `success`
|
469
485
|
|
486
|
+
> [!IMPORTANT]
|
487
|
+
> Do **NOT** define a call method in this class. The sequence logic is
|
488
|
+
> automatically defined by the sequence class.
|
489
|
+
|
470
490
|
```ruby
|
471
491
|
class ProcessCheckout < Lite::Command::Sequence
|
472
492
|
|
@@ -477,8 +497,7 @@ class ProcessCheckout < Lite::Command::Sequence
|
|
477
497
|
step SendConfirmationEmail, SendConfirmationText
|
478
498
|
step NotifyWarehouse, unless: proc { ctx.invoice.fullfilled_by_amazon? }
|
479
499
|
|
480
|
-
# Do NOT
|
481
|
-
# Its defined by Lite::Command::Sequence
|
500
|
+
# Do NOT define a call method.
|
482
501
|
|
483
502
|
private
|
484
503
|
|
@@ -488,7 +507,7 @@ class ProcessCheckout < Lite::Command::Sequence
|
|
488
507
|
|
489
508
|
end
|
490
509
|
|
491
|
-
|
510
|
+
seq = ProcessCheckout.call(...)
|
492
511
|
# <ProcessCheckout ...>
|
493
512
|
```
|
494
513
|
|
@@ -82,8 +82,17 @@ module Lite
|
|
82
82
|
@errors << "#{method_name} type invalid"
|
83
83
|
end
|
84
84
|
|
85
|
+
def empty?
|
86
|
+
r = Utils.try(options[:filled], :[], :empty)
|
87
|
+
return false if r.nil? || r == true
|
88
|
+
return false unless value.respond_to?(:empty?)
|
89
|
+
|
90
|
+
value.empty?
|
91
|
+
end
|
92
|
+
|
85
93
|
def validate_attribute_filled!
|
86
|
-
return unless filled?
|
94
|
+
return unless filled?
|
95
|
+
return unless value.nil? || empty?
|
87
96
|
|
88
97
|
@errors << "#{method_name} must be filled"
|
89
98
|
end
|
data/lib/lite/command/base.rb
CHANGED
@@ -5,6 +5,8 @@ module Lite
|
|
5
5
|
class Base
|
6
6
|
|
7
7
|
def self.inherited(base)
|
8
|
+
super
|
9
|
+
|
8
10
|
base.include Lite::Command::Internals::Context
|
9
11
|
base.include Lite::Command::Internals::Call
|
10
12
|
base.include Lite::Command::Internals::Execute
|
@@ -21,8 +23,6 @@ module Lite
|
|
21
23
|
#{base}::Failure = Class.new(#{base}::Fault)
|
22
24
|
#{base}::Error = Class.new(#{base}::Fault)
|
23
25
|
RUBY
|
24
|
-
|
25
|
-
super
|
26
26
|
end
|
27
27
|
|
28
28
|
attr_reader :context
|
data/lib/lite/command/version.rb
CHANGED
data/lite-command.gemspec
CHANGED
@@ -40,7 +40,6 @@ Gem::Specification.new do |spec|
|
|
40
40
|
spec.add_dependency "ostruct"
|
41
41
|
|
42
42
|
spec.add_development_dependency "bundler"
|
43
|
-
spec.add_development_dependency "fasterer"
|
44
43
|
spec.add_development_dependency "generator_spec"
|
45
44
|
spec.add_development_dependency "rake"
|
46
45
|
spec.add_development_dependency "rspec"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lite-command
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.1.
|
4
|
+
version: 2.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Juan Gomez
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-10-
|
11
|
+
date: 2024-10-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ostruct
|
@@ -38,20 +38,6 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: fasterer
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - ">="
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '0'
|
48
|
-
type: :development
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - ">="
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '0'
|
55
41
|
- !ruby/object:Gem::Dependency
|
56
42
|
name: generator_spec
|
57
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -171,12 +157,11 @@ executables: []
|
|
171
157
|
extensions: []
|
172
158
|
extra_rdoc_files: []
|
173
159
|
files:
|
174
|
-
- ".fasterer.yml"
|
175
160
|
- ".gitattributes"
|
161
|
+
- ".github/workflows/ci.yml"
|
176
162
|
- ".gitignore"
|
177
163
|
- ".rspec"
|
178
164
|
- ".rubocop.yml"
|
179
|
-
- ".travis.yml"
|
180
165
|
- CHANGELOG.md
|
181
166
|
- CODE_OF_CONDUCT.md
|
182
167
|
- Gemfile
|
data/.fasterer.yml
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
speedups:
|
2
|
-
block_vs_symbol_to_proc: true
|
3
|
-
each_with_index_vs_while: false
|
4
|
-
fetch_with_argument_vs_block: true
|
5
|
-
for_loop_vs_each: true
|
6
|
-
getter_vs_attr_reader: true
|
7
|
-
gsub_vs_tr: true
|
8
|
-
hash_merge_bang_vs_hash_brackets: true
|
9
|
-
keys_each_vs_each_key: true
|
10
|
-
map_flatten_vs_flat_map: true
|
11
|
-
module_eval: true
|
12
|
-
proc_call_vs_yield: true
|
13
|
-
rescue_vs_respond_to: true
|
14
|
-
reverse_each_vs_reverse_each: true
|
15
|
-
select_first_vs_detect: true
|
16
|
-
select_last_vs_reverse_detect: true
|
17
|
-
setter_vs_attr_writer: true
|
18
|
-
shuffle_first_vs_sample: true
|
19
|
-
sort_vs_sort_by: true
|
data/.travis.yml
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
sudo: false
|
2
|
-
language: ruby
|
3
|
-
cache: bundler
|
4
|
-
rvm:
|
5
|
-
- 2.5
|
6
|
-
- 2.6
|
7
|
-
- 2.7
|
8
|
-
- ruby-head
|
9
|
-
matrix:
|
10
|
-
fast_finish: true
|
11
|
-
allow_failures:
|
12
|
-
- rvm: ruby-head
|
13
|
-
before_install:
|
14
|
-
- gem update --system
|
15
|
-
- gem install bundler
|
16
|
-
install:
|
17
|
-
- bundle install --jobs=3 --retry=3
|
18
|
-
script:
|
19
|
-
- bundle exec rspec
|
20
|
-
- bundle exec rubocop
|
21
|
-
- bundle exec fasterer
|
22
|
-
notifications:
|
23
|
-
email: false
|
24
|
-
slack:
|
25
|
-
secure: 2PptrBf+yl8evd/WA4XKg0tzJqSdEn5TaAhuHR90o5X6Rd0MA4SES+QZQn9A81CPuJk//V8Dur8NQQzrcAivVOsmC+JbIHZV1MS8v32Uvoy28Vr8omAne4Ec3so6rXCIMniREGcHtKeXHdNYO61VZPPjgxd7RTwajvTC7KS0ViuKBPct+hq+WfJmX6Xp2vxTHImNVurExBrFZkJyPtV+UG1tJ6mB+kF8MQ1+t3ZBtU4+6eVviADYi7FBEeP0j9gdKBCzU7aPoE+XLMWcP0tI9ibaP9crEMKi05JNDLzm5f6PGvGJWEQSCeh3CxNhaEF7eog3BAw6mUXkKI0hvFfrZeyG+VkDU6T9rAG8u55BrREi4mRNJl0c0rS2J45bFRmYOlvDNn9xrhMkkfFn8SMX3etYJYqHNBxdF2TX4SzpDrshd3hcm2btmkThn/Ua/mYUWs9OF3O/ZSu0zB+/g83zrAsjMyfqJ1PE5ZiiMD2Rjrzb/fyYcsNeVhncwRdse9LtJRqz1KpOh/r5LBrWfRRCQwlVNwMtBdFxQ7Ytl9dz/dfJmGKgM8zsdJdI6ZyV+lwa0jPaql6Y8wXjox1wwN3IRCyvoVsgxmiQAV8jnWD2lf6I/Ea5yXCImGluuRAyRrAizElXGBNubhAVtQwQinHWfaYO+mJElScBwrxTVXuzQUI=
|