handleit 1.0.2 → 1.0.3
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/lib/handle.rb +18 -5
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bc8973c8aea21d1b2d6db25c214f7ce80b4741cedc025f6ac2296b4da5e33108
|
4
|
+
data.tar.gz: f2cd9f9c8829b509c74810ba4e1dd035279528e45876bbca898e7318fc01453d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6306444255fbb92cf68507eb53190abac556d90c9199ae3716e9cfd30e251d305b4bcc55cc57b5f544ff0cca8850f4aa318a835863d0f7b739c30baf7156f39d
|
7
|
+
data.tar.gz: fe2d5dd2272fd75d1f04cfa0f85b90867695bfcf12824da4f5105745dd9d3ddedbb7035a36aa49bd3c68042a1cdc9335b998e1185d813cae5ccecad04e447bf9
|
data/lib/handle.rb
CHANGED
@@ -1,13 +1,17 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class Handle
|
4
|
+
NotValidError = Class.new(StandardError)
|
5
|
+
|
4
6
|
class << self
|
5
|
-
def it(&block)
|
6
|
-
new.it(&block)
|
7
|
+
def it(options={}, &block)
|
8
|
+
new.it(options, &block)
|
7
9
|
end
|
8
10
|
end
|
9
11
|
|
10
|
-
def it
|
12
|
+
def it(options)
|
13
|
+
validate(options)
|
14
|
+
|
11
15
|
@result = OpenStruct.new return: yield, success: true, error: nil
|
12
16
|
self
|
13
17
|
rescue StandardError => e
|
@@ -16,12 +20,12 @@ class Handle
|
|
16
20
|
end
|
17
21
|
|
18
22
|
def with
|
19
|
-
@result.return = yield(@result.return, **options = {}) if
|
23
|
+
@result.return = yield(@result.return, **options = {}) if success?
|
20
24
|
self
|
21
25
|
end
|
22
26
|
|
23
27
|
def on_fail
|
24
|
-
yield(@result.error, **options = {}) unless
|
28
|
+
yield(@result.error, **options = {}) unless success?
|
25
29
|
self
|
26
30
|
end
|
27
31
|
|
@@ -36,4 +40,13 @@ class Handle
|
|
36
40
|
def error
|
37
41
|
@result.error
|
38
42
|
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
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
|
50
|
+
raise NotValidError, condition_error unless good_to_go
|
51
|
+
end
|
39
52
|
end
|