keka 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +3 -0
- data/Gemfile.lock +1 -1
- data/README.md +21 -2
- data/lib/keka.rb +27 -5
- data/lib/keka/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5964f3bdc33f5b4367afacac369297ab87c48ce41bc43123977622fc33718166
|
4
|
+
data.tar.gz: ac2a02bc9853226d5e967f16ae0dba0629e17660d8ac9aae8711e6c5b5111481
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6507a7aa94118fb84b1e21246d5d30b165e0851e9354a30cebe5a090c85e518d05eeb0285264728df81bca99ce9e5d44d9f92e074cb68a8d1a4a8619e81a3a7b
|
7
|
+
data.tar.gz: e30427e399de612b9797ee5bd8d6b1bb443ac64b391c27870b6454e3a98a8020c1704a02a0734b0a3b589f1b99b329283ebea49710c663d68fe615f6d755cb79
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -18,8 +18,8 @@ class Order
|
|
18
18
|
Keka.run do
|
19
19
|
# returns an err keka with provided msg if !refundable?
|
20
20
|
Keka.err_unless!(refundable?, 'Payment is no longer refundable.')
|
21
|
-
# returns an err keka with provided
|
22
|
-
Keka.err_unless!(payment.refund
|
21
|
+
# returns an err keka with value from provided block if refund does not exist
|
22
|
+
Keka.err_unless!(payment.refund) { CustomErrorClass.new(message: 'Already been refunded') }
|
23
23
|
# execute statements if nothing 'return' from above
|
24
24
|
do_something_else
|
25
25
|
# if cancel_delivery
|
@@ -42,6 +42,13 @@ class Order
|
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
|
+
class CustomErrorClass
|
46
|
+
def initialize(message:)
|
47
|
+
@message = message
|
48
|
+
Airbrake.notify('Received refund request for already refunded method')
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
45
52
|
class SomeController
|
46
53
|
def some_action
|
47
54
|
keka = @order.refund
|
@@ -57,6 +64,18 @@ end
|
|
57
64
|
Of course, you can also use `.err_unless!`, `.err_if!`, and `.ok_if!` outside
|
58
65
|
of the `Keka.run` block.
|
59
66
|
|
67
|
+
### Passing a block
|
68
|
+
You can also pass a block to be executed if the result short circuits the execution. This is very useful for when you want to return a more complex `msg` than a String. For example, if you want to return an error class that validates certain properties upon initialization, then you will want to create the instance of error class in the block.
|
69
|
+
|
70
|
+
NOTE: This block will only run if there is no `msg` provided.
|
71
|
+
|
72
|
+
```
|
73
|
+
Keka.err_if!(true) { raise Error } # Raises error
|
74
|
+
Keka.err_if!(false) { raise Error } # Does not raises error
|
75
|
+
Keka.err_if!(true, 'msg') { raise Error } # Does not raises error
|
76
|
+
Keka.err_if!(false, 'msg') { raise Error } # Does not raises error
|
77
|
+
```
|
78
|
+
|
60
79
|
### Abort Unconditionally
|
61
80
|
|
62
81
|
Sometimes you know you want to abort, but you also need to a few things before aborting, such as saving the error result to database, logging, or submitting a metric to your monitoring service. You can use `.err!` or `.ok!` methods to abort from the current `Keka.run` block. Both methods can be invoked with or without a message argument.
|
data/lib/keka.rb
CHANGED
@@ -7,17 +7,39 @@ module Keka
|
|
7
7
|
class << self
|
8
8
|
include Context::Originable
|
9
9
|
|
10
|
-
def err_if!(evaluator, msg = nil)
|
11
|
-
|
10
|
+
def err_if!(evaluator, msg = nil, &block)
|
11
|
+
if (evaluator.respond_to?(:ok?) ? evaluator.ok? : evaluator)
|
12
|
+
if msg
|
13
|
+
raise Halt.new(err_result(msg))
|
14
|
+
elsif block_given?
|
15
|
+
message = yield block
|
16
|
+
raise Halt.new(err_result(message))
|
17
|
+
else
|
18
|
+
raise Halt.new(err_result)
|
19
|
+
end
|
20
|
+
end
|
12
21
|
end
|
13
22
|
|
14
|
-
def err_unless!(evaluator, msg = nil)
|
23
|
+
def err_unless!(evaluator, msg = nil, &block)
|
15
24
|
if evaluator.is_a? self::Result
|
16
25
|
return if evaluator.ok?
|
17
|
-
|
26
|
+
if msg
|
27
|
+
evaluator.msg = msg
|
28
|
+
else
|
29
|
+
evaluator.msg = yield block if block_given?
|
30
|
+
end
|
18
31
|
raise Halt.new(evaluator)
|
19
32
|
else
|
20
|
-
|
33
|
+
unless evaluator
|
34
|
+
error_result = \
|
35
|
+
if msg
|
36
|
+
err_result(msg)
|
37
|
+
else
|
38
|
+
message = yield block if block_given?
|
39
|
+
err_result(message)
|
40
|
+
end
|
41
|
+
raise Halt.new(error_result)
|
42
|
+
end
|
21
43
|
end
|
22
44
|
end
|
23
45
|
|
data/lib/keka/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: keka
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- zino
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-05-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|