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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/handle.rb +18 -5
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d933d2ca5c478e062517bcf1fcba6bcc7115c50c7bd9a7a890bf96943d73bacd
4
- data.tar.gz: a176c0572e9d83bccfb3831e1dd1a56808f338c0f22e562edc198d31f37f1504
3
+ metadata.gz: bc8973c8aea21d1b2d6db25c214f7ce80b4741cedc025f6ac2296b4da5e33108
4
+ data.tar.gz: f2cd9f9c8829b509c74810ba4e1dd035279528e45876bbca898e7318fc01453d
5
5
  SHA512:
6
- metadata.gz: 6407bc53612c5c23984767f63d99ef8d870b42b21e91618bb6e3252c2740f9880cd19e347c0af34d341832935124e487ec0122d775e61958928612d5ce7945b7
7
- data.tar.gz: 9342245ee1f24a96b95bf3505d871feb0be04b1e36a63276b8edc76b647e5932a3213ea42ac7b75c84f07f617b97f9a2f71f564c4a2b59661ae7cb9fc28ca715
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 @result.success
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 @result.success
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
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.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oleg Saltykov