handleit 1.0.3 → 1.2.5

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/handle.rb +24 -9
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bc8973c8aea21d1b2d6db25c214f7ce80b4741cedc025f6ac2296b4da5e33108
4
- data.tar.gz: f2cd9f9c8829b509c74810ba4e1dd035279528e45876bbca898e7318fc01453d
3
+ metadata.gz: c980fa58183dc34defe868759209c22ffc643e34b817d172b9a85cc103e93710
4
+ data.tar.gz: 560d86343cf2073f1c7cb05d3a1377bcd240f93fb2ac9d5492cf3a0b644b2edc
5
5
  SHA512:
6
- metadata.gz: 6306444255fbb92cf68507eb53190abac556d90c9199ae3716e9cfd30e251d305b4bcc55cc57b5f544ff0cca8850f4aa318a835863d0f7b739c30baf7156f39d
7
- data.tar.gz: fe2d5dd2272fd75d1f04cfa0f85b90867695bfcf12824da4f5105745dd9d3ddedbb7035a36aa49bd3c68042a1cdc9335b998e1185d813cae5ccecad04e447bf9
6
+ metadata.gz: 419127c3a8fbc102f2418604919982d7583272ae07b7e01475c782a0819d23aa92d2d71e2f309a1de5f10d541210b2807b2a1ecb74788031dc96b4491c0ff9b1
7
+ data.tar.gz: 255a3dd89c13bf316cdf1f3bddc5790112ecc5203f09faac75b333adb13f2c24685dbcf66f64c7e9feddf2ec181f92c6966946f0678c902eb7dad7516b38268b
data/lib/handle.rb CHANGED
@@ -2,9 +2,9 @@
2
2
 
3
3
  class Handle
4
4
  NotValidError = Class.new(StandardError)
5
-
5
+
6
6
  class << self
7
- def it(options={}, &block)
7
+ def it(options = {}, &block)
8
8
  new.it(options, &block)
9
9
  end
10
10
  end
@@ -13,15 +13,25 @@ class Handle
13
13
  validate(options)
14
14
 
15
15
  @result = OpenStruct.new return: yield, success: true, error: nil
16
+ @returns_pool = []
16
17
  self
17
18
  rescue StandardError => e
18
- @result = OpenStruct.new return: nil, success: false, error: e
19
- self
19
+ error_handler(e)
20
20
  end
21
21
 
22
- def with
23
- @result.return = yield(@result.return, **options = {}) if success?
22
+ def with(args={})
23
+ if success?
24
+ @returns_pool << yield(@result.return, **options = {})
25
+ @result.return = @returns_pool.last
26
+ end
24
27
  self
28
+ rescue StandardError => e
29
+ if args[:on_fail] == :rollback
30
+ @result.return = @returns_pool.last
31
+ self
32
+ else
33
+ error_handler(e)
34
+ end
25
35
  end
26
36
 
27
37
  def on_fail
@@ -43,10 +53,15 @@ class Handle
43
53
 
44
54
  private
45
55
 
56
+ def error_handler(err)
57
+ @result = OpenStruct.new return: nil, success: false, error: err
58
+ self
59
+ end
60
+
46
61
  def validate(options)
47
- condition = options[:when]
48
- condition_error = options[:error] || 'Not Valid!'
49
- good_to_go = condition&.respond_to?(:call) ? condition&.call : true
62
+ condition = options[:when].nil? || options[:when]
63
+ condition_error = options[:error] || options[:not_valid_error] || 'Not Valid!'
64
+ good_to_go = condition&.respond_to?(:call) ? condition&.call : condition
50
65
  raise NotValidError, condition_error unless good_to_go
51
66
  end
52
67
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: handleit
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oleg Saltykov
@@ -40,5 +40,5 @@ rubygems_version: 3.1.4
40
40
  signing_key:
41
41
  specification_version: 4
42
42
  summary: Handleit - promise like way of handling logic execution with ability of chaining
43
- handlers like in Elexir pipelining approach
43
+ handlers like in js promise, plus some useful additions like guard and rollback
44
44
  test_files: []