solid-process 0.1.0 → 0.2.0

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: 1190b2778ef23e74384d8526ff747c3a741c62a23de0308dc0bcce85c427fef6
4
- data.tar.gz: b3dd1f4009959aed58c08943e3ae642f5322c7d989036161bdaa71559c36fa5d
3
+ metadata.gz: d4b6ab233b8f225123d7a74114c281011f94f7a186faeaee54b3d25e9ba51c2e
4
+ data.tar.gz: 57a1de84e07d74971640ce4c03b1e12812d1da7765eb83b6ebb460e92972d1bb
5
5
  SHA512:
6
- metadata.gz: 3e81a5319038e788e726573abf7f53c2fccdd4ad5cd24d8b57db426cc4d63bd3e2c10bcc041d24c32c268b8ad7abfa61e97fbf3394e096974a2ee6c90262ca3a
7
- data.tar.gz: 3bbdadd724e66a871f3ec83701e9feff39f26fbf59886c83954575e16dc80849ae3d5787acffeec01b43acdafdd4b1d2e19a1b12f55503602a4e71e73317866f
6
+ metadata.gz: 378aff92aaacc388198e4c72a5ce0fb72df788b1dd7763d2b0f11024629c8988d95e4e52015393dddd4cfbd1116539f1c6f5e1a541422e296117a27e7a7e778b
7
+ data.tar.gz: 0040c63c885ff1177dd11c864d208a64c2b46dec1bee9bf71ea2e7df5ac2340668ac8e524c848c30b736457c3a64051e0084e01c3d401cf3bbaf1293959e6764
data/CHANGELOG.md CHANGED
@@ -1,5 +1,7 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.0] - 2024-03-18
4
+
3
5
  ## [0.1.0] - 2024-03-16
4
6
 
5
7
  - Initial release
data/lib/solid/input.rb CHANGED
@@ -1,13 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class Solid::Input
4
- require_relative "model"
5
-
6
- def self.inherited(subclass)
7
- subclass.include(::Solid::Model)
8
- end
3
+ require_relative "model"
9
4
 
10
- def self.[](...)
11
- new(...)
12
- end
5
+ class Solid::Input
6
+ include ::Solid::Model
13
7
  end
data/lib/solid/model.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Solid::Model
4
- require_relative "model/access"
3
+ require_relative "model/access"
5
4
 
5
+ module Solid::Model
6
6
  extend ::ActiveSupport::Concern
7
7
 
8
8
  included do
@@ -14,6 +14,16 @@ module Solid::Model
14
14
  include ::ActiveModel::Validations::Callbacks
15
15
  end
16
16
 
17
+ module ClassMethods
18
+ def [](...)
19
+ new(...)
20
+ end
21
+
22
+ def inherited(subclass)
23
+ subclass.include(::Solid::Model)
24
+ end
25
+ end
26
+
17
27
  def inspect
18
28
  "#<#{self.class.name} #{attributes.map { |k, v| "#{k}=#{v.inspect}" }.join(" ")}>"
19
29
  end
@@ -5,37 +5,31 @@ class Solid::Process
5
5
  def self.included(subclass)
6
6
  subclass.include ActiveSupport::Callbacks
7
7
 
8
- subclass.define_callbacks(:success, :failure, :output)
8
+ subclass.define_callbacks(:call, :success, :failure)
9
9
 
10
10
  subclass.extend ClassMethods
11
11
  end
12
12
 
13
13
  module ClassMethods
14
- def after_success(*args, &block)
15
- options = args.extract_options!
16
- options = options.dup
17
- options[:prepend] = true
18
-
19
- set_callback(:success, :after, *args, options, &block)
14
+ def before_call(*filters, &block)
15
+ set_callback(:call, :before, *filters, &block)
20
16
  end
21
17
 
22
- def after_failure(*args, &block)
23
- options = args.extract_options!
24
- options = options.dup
25
- options[:prepend] = true
26
-
27
- set_callback(:failure, :after, *args, options, &block)
18
+ def around_call(*filters, &block)
19
+ set_callback(:call, :around, *filters, &block)
28
20
  end
29
21
 
30
- def after_output(*args, &block)
31
- options = args.extract_options!
32
- options = options.dup
33
- options[:prepend] = true
22
+ def after_call(*filters, &block)
23
+ set_callback(:call, :after, *filters, &block)
24
+ end
34
25
 
35
- set_callback(:output, :after, *args, options, &block)
26
+ def after_success(*filters, &block)
27
+ set_callback(:success, :after, *filters, &block)
36
28
  end
37
29
 
38
- alias_method :after_result, :after_output
30
+ def after_failure(*filters, &block)
31
+ set_callback(:failure, :after, *filters, &block)
32
+ end
39
33
  end
40
34
  end
41
35
  end
@@ -1,31 +1,35 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # rubocop:disable Lint/RescueException
3
4
  class Solid::Process
4
5
  module Caller
5
6
  def call(arg = nil)
6
- if output?
7
- raise Error, "#{self.class}#call already called. " \
8
- "Use #{self.class}#output to access the result or create a new instance to call again."
9
- end
7
+ output_already_set! if output?
8
+
9
+ self.input = arg
10
10
 
11
- self.input = self.class.input.then { arg.instance_of?(_1) ? arg : _1.new(arg) }
11
+ run_callbacks(:call) do
12
+ ::BCDD::Result.event_logs(name: self.class.name) do
13
+ self.output =
14
+ if dependencies&.invalid?
15
+ Failure(:invalid_dependencies, dependencies: dependencies)
16
+ elsif input.invalid?
17
+ Failure(:invalid_input, input: input)
18
+ else
19
+ super(input.attributes.deep_symbolize_keys)
20
+ end
21
+ rescue ::Exception => exception
22
+ rescue_with_handler(exception) || raise
12
23
 
13
- ::BCDD::Result.event_logs(name: self.class.name) do
14
- self.output =
15
- if dependencies&.invalid?
16
- Failure(:invalid_dependencies, dependencies: dependencies)
17
- elsif input.invalid?
18
- Failure(:invalid_input, input: input)
19
- else
20
- super(input.attributes.deep_symbolize_keys)
21
- end
24
+ output
25
+ end
22
26
  end
23
27
 
24
28
  run_callbacks(:success) if output.success?
25
29
  run_callbacks(:failure) if output.failure?
26
- run_callbacks(:output)
27
30
 
28
31
  output
29
32
  end
30
33
  end
31
34
  end
35
+ # rubocop:enable Lint/RescueException
@@ -5,11 +5,7 @@ class Solid::Process
5
5
  def input=(klass)
6
6
  const_defined?(:Input, false) and raise Error, "#{const_get(:Input, false)} class already defined"
7
7
 
8
- unless klass.is_a?(::Class) && klass < ::Solid::Model
9
- raise ArgumentError, "#{klass.inspect} must be a class that includes #{::Solid::Model}"
10
- end
11
-
12
- const_set(:Input, klass)
8
+ const_set(:Input, Config::SolidModel[klass])
13
9
  end
14
10
 
15
11
  def input(&block)
@@ -17,7 +13,7 @@ class Solid::Process
17
13
 
18
14
  block.nil? and raise Error, "#{self}::Input is undefined. Use #{self}.input { ... } to define it."
19
15
 
20
- klass = ::Class.new(::Solid::Input)
16
+ klass = ::Class.new(Config.instance.input_class)
21
17
  klass.class_eval(&block)
22
18
 
23
19
  self.input = klass
@@ -26,11 +22,7 @@ class Solid::Process
26
22
  def dependencies=(klass)
27
23
  const_defined?(:Dependencies, false) and raise Error, "#{const_get(:Dependencies, false)} class already defined"
28
24
 
29
- unless klass.is_a?(::Class) && klass < ::Solid::Model
30
- raise ArgumentError, "#{klass.inspect} must be a class that includes #{::Solid::Model}"
31
- end
32
-
33
- const_set(:Dependencies, klass)
25
+ const_set(:Dependencies, Config::SolidModel[klass])
34
26
  end
35
27
 
36
28
  def dependencies(&block)
@@ -38,7 +30,7 @@ class Solid::Process
38
30
 
39
31
  return if block.nil?
40
32
 
41
- klass = ::Class.new(::Solid::Input)
33
+ klass = ::Class.new(Config.instance.dependencies_class)
42
34
  klass.class_eval(&block)
43
35
 
44
36
  self.dependencies = klass
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Solid::Process
4
+ class Config
5
+ SolidModel = ->(klass) do
6
+ return klass if klass.is_a?(::Class) && klass < ::Solid::Model
7
+
8
+ raise ArgumentError, "#{klass.inspect} must be a class that includes #{::Solid::Model}"
9
+ end
10
+
11
+ attr_reader :input_class, :dependencies_class
12
+
13
+ def initialize
14
+ self.input_class = ::Solid::Input
15
+ self.dependencies_class = ::Solid::Input
16
+ end
17
+
18
+ def input_class=(klass)
19
+ @input_class = SolidModel[klass]
20
+ end
21
+
22
+ def dependencies_class=(klass)
23
+ @dependencies_class = SolidModel[klass]
24
+ end
25
+
26
+ alias_method :deps_class, :dependencies_class
27
+ alias_method :deps_class=, :dependencies_class=
28
+
29
+ class << self
30
+ attr_reader :instance
31
+ end
32
+
33
+ @instance = new
34
+ end
35
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Solid
4
4
  class Process
5
- VERSION = "0.1.0"
5
+ VERSION = "0.2.0"
6
6
  end
7
7
  end
data/lib/solid/process.rb CHANGED
@@ -11,6 +11,7 @@ module Solid
11
11
  class Process
12
12
  require "solid/process/version"
13
13
  require "solid/process/error"
14
+ require "solid/process/config"
14
15
  require "solid/process/caller"
15
16
  require "solid/process/callbacks"
16
17
  require "solid/process/class_methods"
@@ -18,23 +19,46 @@ module Solid
18
19
 
19
20
  extend ClassMethods
20
21
 
22
+ include Callbacks
23
+ include ::ActiveSupport::Rescuable
21
24
  include ::BCDD::Context.mixin(config: {addon: {continue: true}})
22
25
 
23
26
  def self.inherited(subclass)
27
+ super
28
+
24
29
  subclass.prepend(Caller)
25
- subclass.include(Callbacks)
26
30
  end
27
31
 
28
32
  def self.call(arg = nil)
29
33
  new.call(arg)
30
34
  end
31
35
 
32
- attr_accessor :input, :output, :dependencies
36
+ def self.configuration(&block)
37
+ yield config
38
+
39
+ config.freeze
40
+ end
41
+
42
+ def self.config
43
+ Config.instance
44
+ end
33
45
 
34
- private :input=, :output=, :dependencies=
46
+ attr_reader :output, :input, :dependencies
35
47
 
36
48
  def initialize(arg = nil)
37
- self.dependencies = self.class.dependencies&.then { arg.instance_of?(_1) ? arg : _1.new(arg) }
49
+ self.dependencies = arg
50
+ end
51
+
52
+ def call(_arg = nil)
53
+ raise Error, "#{self.class}#call must be implemented."
54
+ end
55
+
56
+ def with(dependencies)
57
+ self.class.new(dependencies.with_indifferent_access.with_defaults(deps&.attributes))
58
+ end
59
+
60
+ def new(dependencies = {})
61
+ with(dependencies)
38
62
  end
39
63
 
40
64
  def input?
@@ -49,8 +73,12 @@ module Solid
49
73
  !dependencies.nil?
50
74
  end
51
75
 
52
- def call(_arg = nil)
53
- raise Error, "#{self.class}#call must be implemented."
76
+ def success?(type = nil)
77
+ !!output&.success?(type)
78
+ end
79
+
80
+ def failure?(type = nil)
81
+ !!output&.failure?(type)
54
82
  end
55
83
 
56
84
  def inspect
@@ -69,5 +97,44 @@ module Solid
69
97
  alias_method :deps?, :dependencies?
70
98
  alias_method :result, :output
71
99
  alias_method :result?, :output?
100
+
101
+ private
102
+
103
+ def dependencies=(arg)
104
+ raise Error, "The `#{self.class}#dependencies` is already set." unless dependencies.nil?
105
+
106
+ @dependencies = self.class.dependencies&.then { arg.instance_of?(_1) ? arg : _1.new(arg) }
107
+ end
108
+
109
+ def input=(arg)
110
+ raise Error, "The `#{self.class}#input` is already set." unless input.nil?
111
+
112
+ @input = self.class.input.then { arg.instance_of?(_1) ? arg : _1.new(arg) }
113
+ end
114
+
115
+ def output_already_set!
116
+ raise Error, "The `#{self.class}#output` is already set. " \
117
+ "Use `.output` to access the result or create a new instance to call again."
118
+ end
119
+
120
+ def output=(result)
121
+ output_already_set! unless output.nil?
122
+
123
+ raise Error, "The result #{result.inspect} must be a BCDD::Context." unless result.is_a?(::BCDD::Context)
124
+
125
+ @output = result
126
+ end
127
+
128
+ def Success!(...)
129
+ return self.output = Success(...) if output.nil?
130
+
131
+ raise Error, "`Success!()` cannot be called because the `#{self.class}#output` is already set."
132
+ end
133
+
134
+ def Failure!(...)
135
+ return self.output = Failure(...) if output.nil?
136
+
137
+ raise Error, "`Failure!()` cannot be called because the `#{self.class}#output` is already set."
138
+ end
72
139
  end
73
140
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: solid-process
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Serradura
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-03-16 00:00:00.000000000 Z
11
+ date: 2024-03-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bcdd-result
@@ -92,6 +92,7 @@ files:
92
92
  - lib/solid/process/callbacks.rb
93
93
  - lib/solid/process/caller.rb
94
94
  - lib/solid/process/class_methods.rb
95
+ - lib/solid/process/config.rb
95
96
  - lib/solid/process/error.rb
96
97
  - lib/solid/process/version.rb
97
98
  - lib/solid/result.rb