Class: ServiceActor::Result
- Inherits:
-
BasicObject
- Defined in:
- lib/service_actor/result.rb
Overview
Represents the context of an actor, holding the data from both its inputs and outputs.
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(data = {}) ⇒ Result
Returns a new instance of Result.
35
36
37
|
# File 'lib/service_actor/result.rb', line 35
def initialize(data = {})
@data = data.to_h
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
rubocop:disable Metrics/AbcSize
130
131
132
133
134
135
136
137
138
139
140
141
142
|
# File 'lib/service_actor/result.rb', line 130
def method_missing(method_name, *args) if method_name.end_with?("?") &&
data.key?(key = method_name.to_s.chomp("?").to_sym)
value = data[key]
value.respond_to?(:empty?) ? !value.empty? : !!value
elsif method_name.end_with?("=")
data[method_name.to_s.chomp("=").to_sym] = args.first
elsif data.key?(method_name)
data[method_name]
else
warn_on_undefined_method_invocation(method_name)
end
end
|
Class Method Details
.to_result(data) ⇒ Object
7
8
9
10
11
|
# File 'lib/service_actor/result.rb', line 7
def to_result(data)
return data if data.is_a?(self)
new(data.to_h)
end
|
Instance Method Details
#[](name) ⇒ Object
83
84
85
|
# File 'lib/service_actor/result.rb', line 83
def [](name)
data[name]
end
|
#[]=(key, value) ⇒ Object
87
88
89
|
# File 'lib/service_actor/result.rb', line 87
def []=(key, value)
data[key] = value
end
|
#deconstruct_keys(keys) ⇒ Object
100
101
102
103
104
|
# File 'lib/service_actor/result.rb', line 100
def deconstruct_keys(keys)
deconstructed_keys = to_h.merge(success: success?, failure: failure?)
keys ? deconstructed_keys.slice(*keys) : deconstructed_keys
end
|
#delete!(key) ⇒ Object
91
92
93
|
# File 'lib/service_actor/result.rb', line 91
def delete!(key)
data.delete(key)
end
|
#error ⇒ Object
69
70
71
|
# File 'lib/service_actor/result.rb', line 69
def error
data[:error] || nil
end
|
#fail!(failure_class = nil, result = {}) ⇒ Object
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/service_actor/result.rb', line 49
def fail!(failure_class = nil, result = {})
if failure_class.nil? || failure_class.is_a?(::Hash)
result = failure_class.to_h
failure_class = ::ServiceActor::Failure
end
data.merge!(result)
data[:failure] = true
::Kernel.raise failure_class, self
end
|
#failure? ⇒ Boolean
65
66
67
|
# File 'lib/service_actor/result.rb', line 65
def failure?
data[:failure] || data[:failure?] || false
end
|
#inspect ⇒ Object
Also known as:
pretty_print
43
44
45
|
# File 'lib/service_actor/result.rb', line 43
def inspect
"<#{self.class.name} #{to_h}>"
end
|
#key?(name) ⇒ Boolean
79
80
81
|
# File 'lib/service_actor/result.rb', line 79
def key?(name)
to_h.key?(name)
end
|
#merge!(result) ⇒ Object
73
74
75
76
77
|
# File 'lib/service_actor/result.rb', line 73
def merge!(result)
data.merge!(result)
self
end
|
#respond_to?(method_name, include_private = false) ⇒ Boolean
95
96
97
98
|
# File 'lib/service_actor/result.rb', line 95
def respond_to?(method_name, include_private = false)
self.class.instance_methods.include?(method_name) ||
respond_to_missing?(method_name, include_private)
end
|
#success? ⇒ Boolean
61
62
63
|
# File 'lib/service_actor/result.rb', line 61
def success?
!failure?
end
|
#to_h ⇒ Object
39
40
41
|
# File 'lib/service_actor/result.rb', line 39
def to_h
filter_default_output(data)
end
|