You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The 'code' parameter is used to identify the error (optional).
79
+
Call the service and process a result
62
80
63
-
6. You can process the result received from the service as follows:
64
81
```ruby
65
-
action=MySingleAction.new
66
-
result =action.call(1,2)
67
-
# or uses result.error?
82
+
summator=Summator.new
83
+
result =summator.call(1,2)
84
+
68
85
if result.success?
69
86
result.data
70
87
else
71
-
{
72
-
error_code: result.error_code,
73
-
data: result.data
74
-
}
88
+
result.error_code
75
89
end
76
90
```
77
-
or uses data! if you want to throw an exception
91
+
92
+
Or you can use a `data!` method if you want to throw an exception when an error has occurred:
93
+
78
94
```ruby
79
95
begin
80
-
result =MySingleAction.new.call(1,2).data!
96
+
result =Summator.new.call(1,2).data!
81
97
rescueSingleActionService::InvalidResult => e
82
-
{
83
-
error_code: e.result.error_code,
84
-
data: e.result.data
85
-
}
98
+
e.result.error_code
86
99
end
87
100
```
88
101
102
+
You can define a list of errors of a service by calling a `self.errors(errors_data)` method.<br/>For each error, a method `#{error_name}_error(data = nil)` will be generated to instantiate the result with the appropriate code and optional data:
103
+
104
+
```ruby
105
+
classSummator < SingleActionService::Base
106
+
errors [
107
+
{ name::nil_numbers, code:1 }
108
+
]
109
+
110
+
defcall(x, y)
111
+
if x.nil? || y.nil?
112
+
return nil_numbers_error
113
+
end
114
+
end
115
+
end
116
+
```
117
+
118
+
You can check for the specific error calling an autogenerated checking method of the result:
119
+
120
+
```ruby
121
+
result =Summator.new.call(nil, nil)
122
+
result.nil_numbers_error? # true
123
+
```
124
+
125
+
## API Reference
126
+
127
+
### SingleActionService::Base
128
+
129
+
A base class for services. Create an inheritor from him to use it. Methods:
130
+
131
+
Name |Description
132
+
---------------------------------|-----------
133
+
`success(data = nil)` |Returns a successful `SingleActionService::Result` with<br/>data passed in arguments.
134
+
`error(data: nil, code: nil)` |Returns an error `SingleActionService::Result` with<br/> data and the error code passed in arguments.
135
+
`#{error_name}_error(data = nil)`|Autogenerated method to create an error result <br/>with the specific error code<br/>without having to pass it in arguments.<br/>Generated for each error passed to the `self.errors` method.<br/>Returns an error `SingleActionService::Result` with<br/> data passed to arguments.
136
+
`self.errors(errors_data)` |Call this method to identify possible service errors.<br/>Accepts an array of objects:<br/>`{ name: :error_name, code: :error_code }`
137
+
138
+
### SingleActionService::Result
139
+
140
+
A base class for the result that the service returns. Instantiated by service methods such as `success`, `error` and autogenerated error methods. Methods:
141
+
142
+
Name |Description
143
+
----------------------|-----------
144
+
`success?` |Call this method to check the result for success.<br/>Returns `true` for successful results created by the<br/>`success` method of a service.
145
+
`error?` |Call this method to check the result for error.<br/>Returns `false` for error results created by the<br/>`error` method of a service or by autogenerated<br/> error methods.
146
+
`#{error_name}_error?`|Call this method to check the result for the specific error.<br/>Autogenerated for each error passed to the `self.errors`<br/> of a service.
147
+
`data` |Returns data passed to the result instantiation method
148
+
`data!` |Returns data passed to the result instantiation method.<br/>Throws a `SingleActionService::InvalidResult`<br/>exception if the result contains an error.
149
+
150
+
### SingleActionService::InvalidResult
151
+
152
+
An exception thrown by the `data!` method of a result. Methods:
153
+
154
+
Name |Description
155
+
------------|-----------
156
+
`result` |Returns a `SingleActionService::Result`
157
+
89
158
## Contributing
90
159
91
160
Bug reports and pull requests are welcome on GitHub at https://github.com/sequenia/single_action_service. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
0 commit comments