lite-command 1.5.0 → 2.0.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 +4 -4
- data/.rubocop.yml +23 -4
- data/CHANGELOG.md +8 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +124 -100
- data/README.md +85 -231
- data/Rakefile +2 -2
- data/bin/console +3 -3
- data/lib/generators/rails/command_generator.rb +5 -5
- data/lib/generators/rails/templates/command.rb.tt +1 -1
- data/lib/lite/command/base.rb +49 -0
- data/lib/lite/command/context.rb +32 -0
- data/lib/lite/command/fault.rb +54 -0
- data/lib/lite/command/internals/callable.rb +158 -0
- data/lib/lite/command/internals/executable.rb +83 -0
- data/lib/lite/command/internals/resultable.rb +78 -0
- data/lib/lite/command/version.rb +1 -1
- data/lib/lite/command.rb +9 -13
- data/lite-command.gemspec +27 -29
- metadata +12 -55
- data/lib/lite/command/complex.rb +0 -52
- data/lib/lite/command/exceptions.rb +0 -10
- data/lib/lite/command/extensions/errors.rb +0 -88
- data/lib/lite/command/extensions/memoize.rb +0 -17
- data/lib/lite/command/extensions/propagation.rb +0 -37
- data/lib/lite/command/procedure.rb +0 -51
- data/lib/lite/command/simple.rb +0 -19
- data/lib/lite/command/states.rb +0 -31
@@ -1,88 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'lite/errors' unless defined?(Lite::Errors)
|
4
|
-
|
5
|
-
module Lite
|
6
|
-
module Command
|
7
|
-
module Extensions
|
8
|
-
module Errors
|
9
|
-
|
10
|
-
module ClassMethods
|
11
|
-
|
12
|
-
def perform(*args, **kwargs, &block)
|
13
|
-
instance = call(*args, **kwargs, &block)
|
14
|
-
|
15
|
-
if instance.success?
|
16
|
-
yield(instance.result, Lite::Command::Success, Lite::Command::Failure)
|
17
|
-
else
|
18
|
-
yield(instance.result, Lite::Command::Failure, Lite::Command::Success)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
end
|
23
|
-
|
24
|
-
class << self
|
25
|
-
|
26
|
-
def included(klass)
|
27
|
-
klass.extend(ClassMethods)
|
28
|
-
end
|
29
|
-
|
30
|
-
end
|
31
|
-
|
32
|
-
def errors
|
33
|
-
@errors ||= Lite::Errors::Messages.new
|
34
|
-
end
|
35
|
-
|
36
|
-
def errored?
|
37
|
-
!errors.empty?
|
38
|
-
end
|
39
|
-
|
40
|
-
def fail!
|
41
|
-
raise Lite::Command::ValidationError
|
42
|
-
end
|
43
|
-
|
44
|
-
def failure?
|
45
|
-
called? && errored?
|
46
|
-
end
|
47
|
-
|
48
|
-
def merge_errors!(instance, direction: :from)
|
49
|
-
case direction
|
50
|
-
when :from then errors.merge!(instance.errors)
|
51
|
-
when :to then instance.errors.merge!(errors)
|
52
|
-
end
|
53
|
-
|
54
|
-
nil
|
55
|
-
end
|
56
|
-
|
57
|
-
def merge_exception!(exception, key: :internal)
|
58
|
-
errors.add(key, "#{exception.class} - #{exception.message}")
|
59
|
-
|
60
|
-
nil
|
61
|
-
end
|
62
|
-
|
63
|
-
def result!
|
64
|
-
result if valid?
|
65
|
-
end
|
66
|
-
|
67
|
-
def status
|
68
|
-
return :pending unless called?
|
69
|
-
|
70
|
-
success? ? :success : :failure
|
71
|
-
end
|
72
|
-
|
73
|
-
def success?
|
74
|
-
called? && !errored?
|
75
|
-
end
|
76
|
-
|
77
|
-
def validate!
|
78
|
-
return true if success?
|
79
|
-
|
80
|
-
fail!
|
81
|
-
end
|
82
|
-
|
83
|
-
alias valid? validate!
|
84
|
-
|
85
|
-
end
|
86
|
-
end
|
87
|
-
end
|
88
|
-
end
|
@@ -1,17 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'lite/memoize' unless defined?(Lite::Memoize)
|
4
|
-
|
5
|
-
module Lite
|
6
|
-
module Command
|
7
|
-
module Extensions
|
8
|
-
module Memoize
|
9
|
-
|
10
|
-
def cache
|
11
|
-
@cache ||= Lite::Memoize::Instance.new
|
12
|
-
end
|
13
|
-
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
@@ -1,37 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Lite
|
4
|
-
module Command
|
5
|
-
module Extensions
|
6
|
-
module Propagation
|
7
|
-
|
8
|
-
private
|
9
|
-
|
10
|
-
def assign_and_return!(instance, params)
|
11
|
-
instance.assign_attributes(params)
|
12
|
-
errors.merge!(instance.errors) unless instance.valid?
|
13
|
-
instance
|
14
|
-
end
|
15
|
-
|
16
|
-
def create_and_return!(klass, params)
|
17
|
-
klass = klass.create(params)
|
18
|
-
merge_errors!(klass) unless klass.errors.empty?
|
19
|
-
klass
|
20
|
-
end
|
21
|
-
|
22
|
-
def update_and_return!(instance, params)
|
23
|
-
merge_errors!(instance) unless instance.update(params)
|
24
|
-
instance
|
25
|
-
end
|
26
|
-
|
27
|
-
%i[archive destroy save].each do |action|
|
28
|
-
define_method("#{action}_and_return!") do |instance|
|
29
|
-
merge_errors!(instance) unless instance.send(action)
|
30
|
-
instance
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
@@ -1,51 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Lite
|
4
|
-
module Command
|
5
|
-
class Procedure < Complex
|
6
|
-
|
7
|
-
include Lite::Command::Extensions::Errors
|
8
|
-
|
9
|
-
attr_accessor :exit_on_failure
|
10
|
-
|
11
|
-
def execute
|
12
|
-
steps.each_with_object([]).with_index do |(command, results), i|
|
13
|
-
command.call
|
14
|
-
|
15
|
-
if command.respond_to?(:errors) && command.failure?
|
16
|
-
failed_steps << failed_step(i, command)
|
17
|
-
merge_errors!(command) if respond_to?(:errors)
|
18
|
-
break results if exit_on_failure?
|
19
|
-
else
|
20
|
-
results << command.result
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
def exit_on_failure?
|
26
|
-
@exit_on_failure ||= false
|
27
|
-
end
|
28
|
-
|
29
|
-
def failed_steps
|
30
|
-
@failed_steps ||= []
|
31
|
-
end
|
32
|
-
|
33
|
-
def steps
|
34
|
-
@steps ||= @args.flatten
|
35
|
-
end
|
36
|
-
|
37
|
-
private
|
38
|
-
|
39
|
-
def failed_step(index, command)
|
40
|
-
{
|
41
|
-
index: index,
|
42
|
-
step: index + 1,
|
43
|
-
name: command.class.name,
|
44
|
-
args: command.args,
|
45
|
-
errors: command&.errors&.full_messages
|
46
|
-
}
|
47
|
-
end
|
48
|
-
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
data/lib/lite/command/simple.rb
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Lite
|
4
|
-
module Command
|
5
|
-
class Simple
|
6
|
-
|
7
|
-
class << self
|
8
|
-
|
9
|
-
def call(*args, **kwargs, &block)
|
10
|
-
raise Lite::Command::NotImplementedError unless defined?(execute)
|
11
|
-
|
12
|
-
execute(*args, **kwargs, &block)
|
13
|
-
end
|
14
|
-
|
15
|
-
end
|
16
|
-
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
data/lib/lite/command/states.rb
DELETED
@@ -1,31 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Lite
|
4
|
-
module Command
|
5
|
-
|
6
|
-
class Success
|
7
|
-
|
8
|
-
class << self
|
9
|
-
|
10
|
-
def call
|
11
|
-
yield
|
12
|
-
end
|
13
|
-
|
14
|
-
end
|
15
|
-
|
16
|
-
end
|
17
|
-
|
18
|
-
class Failure
|
19
|
-
|
20
|
-
class << self
|
21
|
-
|
22
|
-
def call
|
23
|
-
# Do nothing
|
24
|
-
end
|
25
|
-
|
26
|
-
end
|
27
|
-
|
28
|
-
end
|
29
|
-
|
30
|
-
end
|
31
|
-
end
|