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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d9aa8d1755bf0535f2daef94a23feca6c60b0ce322ac18b61207b820ea693633
4
- data.tar.gz: b1f9fa5f84161a955e550564906deefc249709a7b2e1032d246413f53cc9d067
3
+ metadata.gz: 5964f3bdc33f5b4367afacac369297ab87c48ce41bc43123977622fc33718166
4
+ data.tar.gz: ac2a02bc9853226d5e967f16ae0dba0629e17660d8ac9aae8711e6c5b5111481
5
5
  SHA512:
6
- metadata.gz: 5f97929bfb8fbfc6a154b4e80991183fbdf043a86ccd2b78bd800fc1c7c68cbdd8389e8840186007d8c3b5b89a8f2516f8b6c75bd8408ef3084edabdb4281b9c
7
- data.tar.gz: 887aa03c1d48f8d304bcf599cb5352e4462e54b647f53697b7a1860d38c21f0a38abef0f164eeff159498845d36f38518c309fe0eea2cc07b0ddab866369ac5e
6
+ metadata.gz: 6507a7aa94118fb84b1e21246d5d30b165e0851e9354a30cebe5a090c85e518d05eeb0285264728df81bca99ce9e5d44d9f92e074cb68a8d1a4a8619e81a3a7b
7
+ data.tar.gz: e30427e399de612b9797ee5bd8d6b1bb443ac64b391c27870b6454e3a98a8020c1704a02a0734b0a3b589f1b99b329283ebea49710c663d68fe615f6d755cb79
data/CHANGELOG.md CHANGED
@@ -2,6 +2,9 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## Version 0.5.0
6
+ * Supports a block parameter for `Keka.err_if!` and `Keka.err_unless!`.
7
+
5
8
  ## Version 0.4.0
6
9
  * Support ActiveModel::Errors as argument to error message.
7
10
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- keka (0.4.0)
4
+ keka (0.5.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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 msg if !refund!
22
- Keka.err_unless!(payment.refund, 'Refund failed. Please try again')
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
- raise Halt.new(err_result(msg)) if (evaluator.respond_to?(:ok?) ? evaluator.ok? : evaluator)
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
- evaluator.msg = msg if msg
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
- raise Halt.new(err_result(msg)) unless evaluator
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
@@ -1,3 +1,3 @@
1
1
  module Keka
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
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.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-01-07 00:00:00.000000000 Z
11
+ date: 2021-05-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler