cuscuz 0.0.1 → 0.2.0
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/CHANGELOG.md +1 -1
- data/CODE_OF_CONDUCT.md +7 -81
- data/LICENSE.txt +1 -1
- data/README.md +310 -11
- data/lib/cuscuz/errors.rb +15 -0
- data/lib/cuscuz/operation.rb +47 -0
- data/lib/cuscuz/result.rb +31 -0
- data/lib/cuscuz/version.rb +1 -3
- data/lib/cuscuz.rb +4 -3
- metadata +29 -15
- data/.standard.yml +0 -3
- data/sig/cuscuz.rbs +0 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 55366339a2c1e70d25578a30893052e46b7e89f4037bfd09b74f9b0ade29e659
|
|
4
|
+
data.tar.gz: ab92be4869076ead412f55a0155aaf72abcd0ce3912880c8f1e5cf6826fa8b94
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c852f860a595ffd4e00ad8437df56c8a725abd55f0f79b27a1080c427afc36425733bc84f723533a0c5600dbe184e6026414a837f42ce6613b098d603e641013
|
|
7
|
+
data.tar.gz: 92c91d0a0e56d8f86bdef46d820dff645bf524d7848b70ee85f5a690f40626e23b7008233016ae41c57a7e5962acc8cb96e6f95bef807b5e9c8c233f3040a3b3
|
data/CHANGELOG.md
CHANGED
data/CODE_OF_CONDUCT.md
CHANGED
|
@@ -1,84 +1,10 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Code of Conduct
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
"cuscuz" follows [The Ruby Community Conduct Guideline](https://www.ruby-lang.org/en/conduct) in all "collaborative space", which is defined as community communications channels (such as mailing lists, submitted patches, commit comments, etc.):
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
* Participants will be tolerant of opposing views.
|
|
6
|
+
* Participants must ensure that their language and actions are free of personal attacks and disparaging personal remarks.
|
|
7
|
+
* When interpreting the words and actions of others, participants should always assume good intentions.
|
|
8
|
+
* Behaviour which can be reasonably considered harassment will not be tolerated.
|
|
6
9
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
## Our Standards
|
|
10
|
-
|
|
11
|
-
Examples of behavior that contributes to a positive environment for our community include:
|
|
12
|
-
|
|
13
|
-
* Demonstrating empathy and kindness toward other people
|
|
14
|
-
* Being respectful of differing opinions, viewpoints, and experiences
|
|
15
|
-
* Giving and gracefully accepting constructive feedback
|
|
16
|
-
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
|
17
|
-
* Focusing on what is best not just for us as individuals, but for the overall community
|
|
18
|
-
|
|
19
|
-
Examples of unacceptable behavior include:
|
|
20
|
-
|
|
21
|
-
* The use of sexualized language or imagery, and sexual attention or
|
|
22
|
-
advances of any kind
|
|
23
|
-
* Trolling, insulting or derogatory comments, and personal or political attacks
|
|
24
|
-
* Public or private harassment
|
|
25
|
-
* Publishing others' private information, such as a physical or email
|
|
26
|
-
address, without their explicit permission
|
|
27
|
-
* Other conduct which could reasonably be considered inappropriate in a
|
|
28
|
-
professional setting
|
|
29
|
-
|
|
30
|
-
## Enforcement Responsibilities
|
|
31
|
-
|
|
32
|
-
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
|
33
|
-
|
|
34
|
-
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
|
35
|
-
|
|
36
|
-
## Scope
|
|
37
|
-
|
|
38
|
-
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
|
39
|
-
|
|
40
|
-
## Enforcement
|
|
41
|
-
|
|
42
|
-
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at 3025661+stephannv@users.noreply.github.com. All complaints will be reviewed and investigated promptly and fairly.
|
|
43
|
-
|
|
44
|
-
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
|
45
|
-
|
|
46
|
-
## Enforcement Guidelines
|
|
47
|
-
|
|
48
|
-
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
|
49
|
-
|
|
50
|
-
### 1. Correction
|
|
51
|
-
|
|
52
|
-
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
|
53
|
-
|
|
54
|
-
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
|
55
|
-
|
|
56
|
-
### 2. Warning
|
|
57
|
-
|
|
58
|
-
**Community Impact**: A violation through a single incident or series of actions.
|
|
59
|
-
|
|
60
|
-
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
|
61
|
-
|
|
62
|
-
### 3. Temporary Ban
|
|
63
|
-
|
|
64
|
-
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
|
65
|
-
|
|
66
|
-
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
|
67
|
-
|
|
68
|
-
### 4. Permanent Ban
|
|
69
|
-
|
|
70
|
-
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
|
71
|
-
|
|
72
|
-
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
|
73
|
-
|
|
74
|
-
## Attribution
|
|
75
|
-
|
|
76
|
-
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
|
77
|
-
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
|
78
|
-
|
|
79
|
-
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
|
80
|
-
|
|
81
|
-
[homepage]: https://www.contributor-covenant.org
|
|
82
|
-
|
|
83
|
-
For answers to common questions about this code of conduct, see the FAQ at
|
|
84
|
-
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
|
10
|
+
If you have any concerns about behaviour within this project, please contact us at ["contact@gunbolt.org"](mailto:"contact@gunbolt.org").
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
|
@@ -1,24 +1,320 @@
|
|
|
1
1
|
# Cuscuz
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
Cuscuz is a gem for writing business logic objects, similar to interactors,
|
|
4
|
+
services, operations, use cases, mutations, and commands. It relies heavily on
|
|
5
|
+
the [Literal](https://literal.fun) gem to provide input and output validation.
|
|
6
|
+
Cuscuz was extracted from our project [pikoin](https://github.com/gunbolt/pikoin),
|
|
7
|
+
a simple personal finance tracker.
|
|
6
8
|
|
|
7
9
|
## Installation
|
|
8
10
|
|
|
9
|
-
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
|
10
|
-
|
|
11
11
|
Install the gem and add to the application's Gemfile by executing:
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
```bash
|
|
14
|
+
bundle add cuscuz --source "https://beta.gem.coop/@gunbolt"
|
|
15
|
+
```
|
|
14
16
|
|
|
15
|
-
|
|
17
|
+
Or put in your Gemfile:
|
|
18
|
+
```ruby
|
|
19
|
+
gem "cuscuz", source: "https://beta.gem.coop/@gunbolt"
|
|
20
|
+
```
|
|
16
21
|
|
|
17
|
-
|
|
22
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
23
|
+
```bash
|
|
24
|
+
gem install cuscuz --source "https://beta.gem.coop/@gunbolt"
|
|
25
|
+
```
|
|
18
26
|
|
|
19
27
|
## Usage
|
|
20
28
|
|
|
21
|
-
|
|
29
|
+
## TL;DR:
|
|
30
|
+
|
|
31
|
+
```ruby
|
|
32
|
+
class UpdateRecord < Cuscuz::Operation
|
|
33
|
+
prop :id, Integer
|
|
34
|
+
prop :attributes, Hash
|
|
35
|
+
|
|
36
|
+
Success = Result.success(record: Record)
|
|
37
|
+
Failure = Result.failure(reason: String, record: Record)
|
|
38
|
+
|
|
39
|
+
def call
|
|
40
|
+
record = Record.find(@id)
|
|
41
|
+
|
|
42
|
+
if record.update(@attributes)
|
|
43
|
+
Success[record:]
|
|
44
|
+
else
|
|
45
|
+
Failure[reason: "Invalid record", record:]
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
result = UpdateRecord.call(id: 1, attributes: {title: "Updated Record"})
|
|
51
|
+
|
|
52
|
+
result.success? # => true
|
|
53
|
+
result.failure? # => false
|
|
54
|
+
result.record # => #<Record id=1 ...>
|
|
55
|
+
result.class # => UpdateUser::Success
|
|
56
|
+
|
|
57
|
+
case result
|
|
58
|
+
in success: true, record:
|
|
59
|
+
...
|
|
60
|
+
in failure: true, reason:, record:
|
|
61
|
+
...
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
case result
|
|
65
|
+
in UpdateRecord::Success(record:)
|
|
66
|
+
...
|
|
67
|
+
in UpdateRecord::Failure(reason:, record:)
|
|
68
|
+
...
|
|
69
|
+
end
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Defining inputs
|
|
73
|
+
Cuscuz uses [Literal::Properties](https://literal.fun/docs/properties.html) to
|
|
74
|
+
enable input validation for operations. You can read the Literal documentation
|
|
75
|
+
to understand the full API, but here are some basic usage examples:
|
|
76
|
+
|
|
77
|
+
```ruby
|
|
78
|
+
# required keyword argument of type Integer
|
|
79
|
+
prop :id, Integer
|
|
80
|
+
|
|
81
|
+
# required keyword argument of type Integer or String
|
|
82
|
+
prop :id, _Union(Integer, String)
|
|
83
|
+
|
|
84
|
+
# optional keyword argument of type Symbol
|
|
85
|
+
prop :type, _String?
|
|
86
|
+
|
|
87
|
+
# optional keyword argument of type custom type
|
|
88
|
+
prop :user, _Nilable(User)
|
|
89
|
+
|
|
90
|
+
# optional keyword argument with default value
|
|
91
|
+
prop :priority, Integer, default: 5
|
|
92
|
+
|
|
93
|
+
# array
|
|
94
|
+
prop :values, Array
|
|
95
|
+
|
|
96
|
+
# advanced array
|
|
97
|
+
prop :posts, _Array(Post)
|
|
98
|
+
|
|
99
|
+
# hash
|
|
100
|
+
prop :attributes, Hash
|
|
101
|
+
|
|
102
|
+
# advanced hash
|
|
103
|
+
prop :options, _Hash(Symbol, _Union(String, Symbol, Integer, _Boolean))
|
|
104
|
+
|
|
105
|
+
# Rails relations
|
|
106
|
+
prop :posts, ActiveRecord::Relation(Post)
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
If you do not provide the correct arguments to the operation, meaningful errors
|
|
110
|
+
will be raised:
|
|
111
|
+
```ruby
|
|
112
|
+
class Sum < Cuscuz::Operation
|
|
113
|
+
prop :a, Integer
|
|
114
|
+
prop :b, Integer
|
|
115
|
+
|
|
116
|
+
# ...
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
Sum.call(a: "2", b: 3)
|
|
120
|
+
# Type mismatch (Literal::TypeError)
|
|
121
|
+
#
|
|
122
|
+
# Sum#initialize (from )
|
|
123
|
+
# a:
|
|
124
|
+
# Expected: Integer
|
|
125
|
+
# Actual (String): "2"
|
|
126
|
+
|
|
127
|
+
Sum.call(a: 2)
|
|
128
|
+
# missing keyword: :b (ArgumentError)
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
The inputs can be accessed via instance variables:
|
|
132
|
+
```ruby
|
|
133
|
+
class Sum < Cuscuz::Operation
|
|
134
|
+
prop :a, Integer
|
|
135
|
+
prop :b, Integer
|
|
136
|
+
|
|
137
|
+
# ...
|
|
138
|
+
|
|
139
|
+
def call
|
|
140
|
+
puts @a
|
|
141
|
+
puts @b
|
|
142
|
+
|
|
143
|
+
# ...
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
Sum.call(a: 5, b: 4)
|
|
148
|
+
# => 5
|
|
149
|
+
# => 4
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## Defining the outputs
|
|
153
|
+
Cuscuz operations are required to return either a `Cuscuz::Result::Success` or
|
|
154
|
+
a `Cuscuz::Result::Failure`. They are subclasses of `Literal::Data`, similar to
|
|
155
|
+
Ruby’s native `Data` class, but with support for property validation through
|
|
156
|
+
Literal features. You can define them using `Result.success` and
|
|
157
|
+
`Result.failure`:
|
|
158
|
+
|
|
159
|
+
```ruby
|
|
160
|
+
class Divide < Cuscuz::Operation
|
|
161
|
+
prop :a, Integer
|
|
162
|
+
prop :b, Integer
|
|
163
|
+
|
|
164
|
+
Success = Result.success(value: Integer)
|
|
165
|
+
Failure = Result.failure(reason: String)
|
|
166
|
+
|
|
167
|
+
def call
|
|
168
|
+
if b == 0
|
|
169
|
+
Failure[reason: "Cannot divide by zero"]
|
|
170
|
+
else
|
|
171
|
+
Success[value: a / b]
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
result = Divide.call(a: 10, b: 5)
|
|
177
|
+
result.success? # => true
|
|
178
|
+
result.failure? # => false
|
|
179
|
+
result.value # => 2
|
|
180
|
+
result.class # => Divide::Success
|
|
181
|
+
|
|
182
|
+
result = Divide.call(a: 10, b: 0)
|
|
183
|
+
result.success? # => false
|
|
184
|
+
result.failure? # => true
|
|
185
|
+
result.reason # => "Cannot divide by zero"
|
|
186
|
+
result.class # => Divide::Failure
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
The result object provides the `#success?` and `#failure?` methods, along with
|
|
190
|
+
accessors for its properties.
|
|
191
|
+
|
|
192
|
+
To instantiate results, you can use either the `.new` or `.[]` method:
|
|
193
|
+
```ruby
|
|
194
|
+
Success[value: 2] == Success.new(value: 2) # => true
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
If you do not provide the correct arguments, an error will be raised:
|
|
198
|
+
```ruby
|
|
199
|
+
Success[value: "2"]
|
|
200
|
+
# Type mismatch (Literal::TypeError)
|
|
201
|
+
#
|
|
202
|
+
# Divide::Success#initialize (from )
|
|
203
|
+
# value:
|
|
204
|
+
# Expected: Integer
|
|
205
|
+
# Actual (String): "2"
|
|
206
|
+
|
|
207
|
+
Success[] # missing keyword: :value (ArgumentError)
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
If your operation does not return a `Cuscuz::Result::Success` or
|
|
211
|
+
`Cuscuz::Result::Failure`, an error will be raised:
|
|
212
|
+
```ruby
|
|
213
|
+
class MyOperation < Cuscuz::Operation
|
|
214
|
+
def call
|
|
215
|
+
return "2"
|
|
216
|
+
end
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
MyOperation.call
|
|
220
|
+
# Cuscuz::InvalidResultTypeError:
|
|
221
|
+
# The operation returned a `String`, it was expected a
|
|
222
|
+
# `Cuscuz::Result::Success` or `Cuscuz::Result::Failure`
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
You can define multiple types of successes and/or failures:
|
|
226
|
+
```ruby
|
|
227
|
+
class CreateUser < Cuscuz::Operation
|
|
228
|
+
prop :attributes, Hash
|
|
229
|
+
|
|
230
|
+
ReadyUserResult = Result.success(user:)
|
|
231
|
+
PendingApprovalResult = Result.success(user:)
|
|
232
|
+
InvalidAttributesResult = Result.failure(user:)
|
|
233
|
+
BannedUserResult = Result.failure(user:)
|
|
234
|
+
|
|
235
|
+
def call
|
|
236
|
+
user = User.new(@attributes)
|
|
237
|
+
|
|
238
|
+
if banned_emails.include?(user.email)
|
|
239
|
+
return BannedUserResult[user:]
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
if user.save
|
|
243
|
+
if user.workspace.needs_approval?
|
|
244
|
+
PendingApprovalResult[user:]
|
|
245
|
+
else
|
|
246
|
+
ReadyUserResult[user:]
|
|
247
|
+
end
|
|
248
|
+
else
|
|
249
|
+
InvalidAttributesResult[user:]
|
|
250
|
+
end
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
private
|
|
254
|
+
|
|
255
|
+
def banned_emails = ...
|
|
256
|
+
end
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
## Executing the operation
|
|
260
|
+
Define an instance method named `#call` inside your operation to implement your
|
|
261
|
+
business logic.
|
|
262
|
+
|
|
263
|
+
You can execute the operation using the `.call` and `.call!` methods. When
|
|
264
|
+
using `.call!`, an error will be raised if the operation fails. For example:
|
|
265
|
+
```ruby
|
|
266
|
+
class InviteUser < Cuscuz::Operation
|
|
267
|
+
prop :role, String
|
|
268
|
+
|
|
269
|
+
Success = Result.success
|
|
270
|
+
Failure = Result.failure
|
|
271
|
+
|
|
272
|
+
def call
|
|
273
|
+
if @role == "admin"
|
|
274
|
+
Success[]
|
|
275
|
+
else
|
|
276
|
+
Failure[]
|
|
277
|
+
end
|
|
278
|
+
end
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
InviteUser.call(role: "admin") # InviteUser::Success
|
|
282
|
+
InviteUser.call(role: "member") # InviteUser::Failure
|
|
283
|
+
|
|
284
|
+
InviteUser.call!(role: "admin") # InviteUser::Success
|
|
285
|
+
InviteUser.call!(role: "member") # raises Cuscuz::FailedOperationError("`InviteUser` execution failed")
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
## Handle operation result
|
|
289
|
+
You can handle the operation result using `#success?` / `#failure?` or pattern
|
|
290
|
+
matching. For example:
|
|
291
|
+
|
|
292
|
+
```ruby
|
|
293
|
+
# success?/failure?
|
|
294
|
+
result = CreateUser.call(attributes: user_attributes)
|
|
295
|
+
|
|
296
|
+
if result.success?
|
|
297
|
+
puts result.user
|
|
298
|
+
else
|
|
299
|
+
puts result.reason
|
|
300
|
+
end
|
|
301
|
+
|
|
302
|
+
# Pattern matching (deconstructing keys)
|
|
303
|
+
case CreateUser.call(attributes: user_attributes)
|
|
304
|
+
in success: true, user:
|
|
305
|
+
puts user
|
|
306
|
+
in failure: true, reason:
|
|
307
|
+
puts reason
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
# Pattern matching (alternative)
|
|
311
|
+
case CreateUser.call(attributes: user_attributes)
|
|
312
|
+
in CreateUser::Succcess(user:)
|
|
313
|
+
puts user
|
|
314
|
+
in CreateUser::Failure(reason:)
|
|
315
|
+
puts reason
|
|
316
|
+
end
|
|
317
|
+
```
|
|
22
318
|
|
|
23
319
|
## Development
|
|
24
320
|
|
|
@@ -28,7 +324,10 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
|
28
324
|
|
|
29
325
|
## Contributing
|
|
30
326
|
|
|
31
|
-
Bug reports and pull requests are welcome on
|
|
327
|
+
Bug reports and pull requests are welcome on Codeberg at https://codeberg.com/gunbolt/cuscuz. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://codeberg.com/gunbolt/cuscuz/blob/master/CODE_OF_CONDUCT.md).
|
|
328
|
+
|
|
329
|
+
Contributions must not include content generated by large language models or other probabilistic tools, including but not limited to Copilot, Claude or ChatGPT. This policy covers code, documentation, pull requests, issues, comments, and any other contributions to the Gunbolt projects.
|
|
330
|
+
|
|
32
331
|
|
|
33
332
|
## License
|
|
34
333
|
|
|
@@ -36,4 +335,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
|
36
335
|
|
|
37
336
|
## Code of Conduct
|
|
38
337
|
|
|
39
|
-
Everyone interacting in the Cuscuz project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://
|
|
338
|
+
Everyone interacting in the Cuscuz project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://codeberg.com/gunbolt/cuscuz/blob/master/CODE_OF_CONDUCT.md).
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
module Cuscuz
|
|
2
|
+
class Error < StandardError; end
|
|
3
|
+
|
|
4
|
+
class InvalidResultTypeError < Error
|
|
5
|
+
def initialize(result_class)
|
|
6
|
+
super("The operation returned a `#{result_class}`, it was expected a `Cuscuz::Result::Success` or `Cuscuz::Result::Failure`")
|
|
7
|
+
end
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
class FailedOperationError < Error
|
|
11
|
+
def initialize(operation_class)
|
|
12
|
+
super("`#{operation_class}` execution failed")
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
module Cuscuz
|
|
2
|
+
class Operation
|
|
3
|
+
extend Literal::Properties
|
|
4
|
+
|
|
5
|
+
Result = Cuscuz::Result
|
|
6
|
+
|
|
7
|
+
RESULT_CLASSES = _Union(Cuscuz::Result::Success, Cuscuz::Result::Failure)
|
|
8
|
+
|
|
9
|
+
def self.call(...) = new(...).call
|
|
10
|
+
|
|
11
|
+
def self.call!(...) = new(...).call!
|
|
12
|
+
|
|
13
|
+
def self.inherited(operation)
|
|
14
|
+
super
|
|
15
|
+
|
|
16
|
+
operation.prepend(Executor)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def call
|
|
20
|
+
raise NoMethodError, "`#call` instance method should be implemented in #{self.class.name}"
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
module Executor
|
|
26
|
+
def call
|
|
27
|
+
result = super
|
|
28
|
+
|
|
29
|
+
if RESULT_CLASSES === result
|
|
30
|
+
result
|
|
31
|
+
else
|
|
32
|
+
raise InvalidResultTypeError.new(result.class)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def call!
|
|
37
|
+
result = call
|
|
38
|
+
|
|
39
|
+
if result.success?
|
|
40
|
+
result
|
|
41
|
+
else
|
|
42
|
+
raise FailedOperationError.new(self.class)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
module Cuscuz
|
|
2
|
+
class Result < Literal::Data
|
|
3
|
+
class Success < Result
|
|
4
|
+
prop :success, _Boolean, default: true, reader: false, predicate: :public do
|
|
5
|
+
true
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
prop :failure, _Boolean, default: false, reader: false, predicate: :public do
|
|
9
|
+
false
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
class Failure < Result
|
|
14
|
+
prop :success, _Boolean, default: false, reader: false, predicate: :public do
|
|
15
|
+
false
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
prop :failure, _Boolean, default: true, reader: false, predicate: :public do
|
|
19
|
+
true
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def self.success(...)
|
|
24
|
+
Success.define(...)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def self.failure(...)
|
|
28
|
+
Failure.define(...)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
data/lib/cuscuz/version.rb
CHANGED
data/lib/cuscuz.rb
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
require "literal"
|
|
2
2
|
|
|
3
|
+
require_relative "cuscuz/errors"
|
|
4
|
+
require_relative "cuscuz/result"
|
|
5
|
+
require_relative "cuscuz/operation"
|
|
3
6
|
require_relative "cuscuz/version"
|
|
4
7
|
|
|
5
8
|
module Cuscuz
|
|
6
|
-
class Error < StandardError; end
|
|
7
|
-
# Your code goes here...
|
|
8
9
|
end
|
metadata
CHANGED
|
@@ -1,37 +1,52 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: cuscuz
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- stephann
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: exe
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
12
|
-
dependencies:
|
|
13
|
-
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: literal
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '0'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '0'
|
|
14
26
|
email:
|
|
15
|
-
-
|
|
27
|
+
- stephann@gunbolt.org
|
|
16
28
|
executables: []
|
|
17
29
|
extensions: []
|
|
18
30
|
extra_rdoc_files: []
|
|
19
31
|
files:
|
|
20
|
-
- ".standard.yml"
|
|
21
32
|
- CHANGELOG.md
|
|
22
33
|
- CODE_OF_CONDUCT.md
|
|
23
34
|
- LICENSE.txt
|
|
24
35
|
- README.md
|
|
25
36
|
- Rakefile
|
|
26
37
|
- lib/cuscuz.rb
|
|
38
|
+
- lib/cuscuz/errors.rb
|
|
39
|
+
- lib/cuscuz/operation.rb
|
|
40
|
+
- lib/cuscuz/result.rb
|
|
27
41
|
- lib/cuscuz/version.rb
|
|
28
|
-
|
|
29
|
-
homepage: https://github.com/stephannv/cuscuz
|
|
42
|
+
homepage: https://codeberg.org/gunbolt/cuscuz
|
|
30
43
|
licenses:
|
|
31
44
|
- MIT
|
|
32
45
|
metadata:
|
|
33
|
-
homepage_uri: https://
|
|
34
|
-
|
|
46
|
+
homepage_uri: https://codeberg.org/gunbolt/cuscuz
|
|
47
|
+
source_code_uri: https://codeberg.org/gunbolt/cuscuz
|
|
48
|
+
changelog_uri: https://codeberg.com/gunbolt/cuscuz/blob/master/CHANGELOG.md
|
|
49
|
+
rubygems_mfa_required: 'true'
|
|
35
50
|
rdoc_options: []
|
|
36
51
|
require_paths:
|
|
37
52
|
- lib
|
|
@@ -39,15 +54,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
39
54
|
requirements:
|
|
40
55
|
- - ">="
|
|
41
56
|
- !ruby/object:Gem::Version
|
|
42
|
-
version: 3.
|
|
57
|
+
version: 3.2.0
|
|
43
58
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
44
59
|
requirements:
|
|
45
60
|
- - ">="
|
|
46
61
|
- !ruby/object:Gem::Version
|
|
47
62
|
version: '0'
|
|
48
63
|
requirements: []
|
|
49
|
-
rubygems_version:
|
|
50
|
-
signing_key:
|
|
64
|
+
rubygems_version: 4.0.6
|
|
51
65
|
specification_version: 4
|
|
52
|
-
summary:
|
|
66
|
+
summary: Cuscuz is a gem for writing business logic objects
|
|
53
67
|
test_files: []
|
data/.standard.yml
DELETED
data/sig/cuscuz.rbs
DELETED