service_actor 1.0.0 → 3.1.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/README.md +234 -91
- data/lib/service_actor.rb +3 -83
- data/lib/service_actor/argument_error.rb +6 -0
- data/lib/{actor → service_actor}/attributable.rb +14 -17
- data/lib/service_actor/base.rb +41 -0
- data/lib/service_actor/collectionable.rb +32 -0
- data/lib/service_actor/conditionable.rb +40 -0
- data/lib/service_actor/core.rb +56 -0
- data/lib/service_actor/defaultable.rb +37 -0
- data/lib/service_actor/error.rb +6 -0
- data/lib/service_actor/failable.rb +40 -0
- data/lib/service_actor/failure.rb +16 -0
- data/lib/service_actor/nil_checkable.rb +51 -0
- data/lib/{actor → service_actor}/playable.rb +10 -9
- data/lib/service_actor/result.rb +55 -0
- data/lib/service_actor/type_checkable.rb +51 -0
- data/lib/service_actor/version.rb +5 -0
- metadata +76 -16
- data/lib/actor/conditionable.rb +0 -34
- data/lib/actor/context.rb +0 -92
- data/lib/actor/defaultable.rb +0 -25
- data/lib/actor/failure.rb +0 -16
- data/lib/actor/filtered_context.rb +0 -49
- data/lib/actor/requireable.rb +0 -36
- data/lib/actor/success.rb +0 -6
- data/lib/actor/type_checkable.rb +0 -43
- data/lib/actor/version.rb +0 -5
data/lib/actor/failure.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class Actor
|
4
|
-
# Error raised when using `fail!` inside an actor.
|
5
|
-
class Failure < StandardError
|
6
|
-
def initialize(context)
|
7
|
-
@context = context
|
8
|
-
|
9
|
-
error = context.respond_to?(:error) ? context.error : nil
|
10
|
-
|
11
|
-
super(error)
|
12
|
-
end
|
13
|
-
|
14
|
-
attr_reader :context
|
15
|
-
end
|
16
|
-
end
|
@@ -1,49 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class Actor
|
4
|
-
# Represents the result of an action, tied to inputs and outputs.
|
5
|
-
class FilteredContext
|
6
|
-
def initialize(context, readers:, setters:)
|
7
|
-
@context = context
|
8
|
-
@readers = readers
|
9
|
-
@setters = setters
|
10
|
-
end
|
11
|
-
|
12
|
-
def inspect
|
13
|
-
"<#{self.class.name} #{context.inspect} " \
|
14
|
-
"readers: #{readers.inspect} " \
|
15
|
-
"setters: #{setters.inspect}>"
|
16
|
-
end
|
17
|
-
|
18
|
-
def fail!(**arguments)
|
19
|
-
context.fail!(**arguments)
|
20
|
-
end
|
21
|
-
|
22
|
-
def succeed!(**arguments)
|
23
|
-
context.fail!(**arguments)
|
24
|
-
end
|
25
|
-
|
26
|
-
private
|
27
|
-
|
28
|
-
attr_reader :context, :readers, :setters
|
29
|
-
|
30
|
-
def method_missing(name, *arguments, **options)
|
31
|
-
return super unless context.respond_to?(name)
|
32
|
-
|
33
|
-
unless available_methods.include?(name)
|
34
|
-
raise ArgumentError, "Cannot call #{name} on #{inspect}"
|
35
|
-
end
|
36
|
-
|
37
|
-
context.public_send(name, *arguments)
|
38
|
-
end
|
39
|
-
|
40
|
-
def respond_to_missing?(name, *_arguments)
|
41
|
-
available_methods.include?(name)
|
42
|
-
end
|
43
|
-
|
44
|
-
def available_methods
|
45
|
-
@available_methods ||=
|
46
|
-
readers + setters.map { |key| "#{key}=".to_sym }
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
data/lib/actor/requireable.rb
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class Actor
|
4
|
-
# Adds `required:` checking to inputs and outputs.
|
5
|
-
#
|
6
|
-
# Example:
|
7
|
-
#
|
8
|
-
# class CreateUser < Actor
|
9
|
-
# input :name, required: true
|
10
|
-
# output :user, required: true
|
11
|
-
# end
|
12
|
-
module Requireable
|
13
|
-
def before
|
14
|
-
super
|
15
|
-
|
16
|
-
check_required_definitions(self.class.inputs, kind: 'Input')
|
17
|
-
end
|
18
|
-
|
19
|
-
def after
|
20
|
-
super
|
21
|
-
|
22
|
-
check_required_definitions(self.class.outputs, kind: 'Output')
|
23
|
-
end
|
24
|
-
|
25
|
-
private
|
26
|
-
|
27
|
-
def check_required_definitions(definitions, kind:)
|
28
|
-
definitions.each do |key, options|
|
29
|
-
next unless options[:required] && @context[key].nil?
|
30
|
-
|
31
|
-
raise ArgumentError,
|
32
|
-
"#{kind} #{key} on #{self.class} is required but was nil."
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
data/lib/actor/success.rb
DELETED
data/lib/actor/type_checkable.rb
DELETED
@@ -1,43 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class Actor
|
4
|
-
# Adds `type:` checking to inputs and outputs. Accepts strings that should
|
5
|
-
# match an ancestor. Also accepts arrays.
|
6
|
-
#
|
7
|
-
# Example:
|
8
|
-
#
|
9
|
-
# class ReduceOrderAmount < Actor
|
10
|
-
# input :order, type: 'Order'
|
11
|
-
# input :amount, type: %w[Integer Float]
|
12
|
-
# input :bonus_applied, type: %w[TrueClass FalseClass]
|
13
|
-
# end
|
14
|
-
module TypeCheckable
|
15
|
-
def before
|
16
|
-
super
|
17
|
-
|
18
|
-
check_type_definitions(self.class.inputs, kind: 'Input')
|
19
|
-
end
|
20
|
-
|
21
|
-
def after
|
22
|
-
super
|
23
|
-
|
24
|
-
check_type_definitions(self.class.outputs, kind: 'Output')
|
25
|
-
end
|
26
|
-
|
27
|
-
private
|
28
|
-
|
29
|
-
def check_type_definitions(definitions, kind:)
|
30
|
-
definitions.each do |key, options|
|
31
|
-
type_definition = options[:type] || next
|
32
|
-
value = @context[key] || next
|
33
|
-
|
34
|
-
types = Array(type_definition).map { |name| Object.const_get(name) }
|
35
|
-
next if types.any? { |type| value.is_a?(type) }
|
36
|
-
|
37
|
-
error = "#{kind} #{key} on #{self.class} must be of type " \
|
38
|
-
"#{types.join(', ')} but was #{value.class}"
|
39
|
-
raise ArgumentError, error
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|