direct 1.1.1 → 1.2.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: 98778b7710dd0641ba88486adc2888551cd823ec4be17494857d42555af8e72a
4
- data.tar.gz: b0216ba7745d33ff0f6cf3f1018472d19a8ec3df00b2678bcda68f4074a125d4
3
+ metadata.gz: b177e702ac1b7caafa4430f8e552336d07997d7ecdb0cc7eb21eda814c5b10a1
4
+ data.tar.gz: 8236f43a4a784720af82eab4b5cbaea76182373cf329a2162001086fa3c8fc64
5
5
  SHA512:
6
- metadata.gz: 1fd862dec144d24f9268574cc91a1fae0a9b90052598d2c3f221b5cc23cd383d60966d600db20ba8551751bd11344a2ca8f79b9563cd834dce941569dc08e0cf
7
- data.tar.gz: 734e1c4f6095c2c5d7566ba607a438000298e28b1c2d352ba9c93a17ea3e7249956119b69b9e1d6184716006fc158ad5394b03e139c44f5f7f7f0972d549af97
6
+ metadata.gz: a065bf3c181084b8fe3418af13dd9b7ea096bd7cb8e6b717e3264457271021591d4bff17eae4e9bb3bb69a4acc4f2304c262a670b77cf69b08d16834ba458e39
7
+ data.tar.gz: 4fe6b632e7c6d6b7204af70efd1cd5e00b53d86a19d5e0cc03f59023bbb9b1c52205693ade062cd3e5a51d2a1eb28c0776d5bb20f77da2d9115f58d74201c63d
@@ -1,3 +1,7 @@
1
+ ## Version 1.2.0
2
+
3
+ Provide a Direct.defer and Direct.strict_defer to allow deferring blocks without success and failure procedures.
4
+
1
5
  ## Version 1.1.1
2
6
 
3
7
  Include Direct.allow_missing_directions to allow as_directed to be ignored when missing.
@@ -1,7 +1,4 @@
1
1
  require "direct/version"
2
- require "direct/executable"
3
- require "direct/group"
4
-
5
2
  # Include this module in your classes to provide a way for
6
3
  # your objects to handle named scenarios with blocks of code.
7
4
  module Direct
@@ -32,20 +29,44 @@ module Direct
32
29
  def self.allow_missing_directions
33
30
  AllowMissing
34
31
  end
32
+ end
33
+
34
+ require "direct/executable"
35
+ require "direct/strict_executable"
36
+ require "direct/group"
35
37
 
38
+ module Direct
36
39
  # Wrap a block of code to return an object for handling
37
- # success or failure.
40
+ # success or failure. Raises exceptions when directions
41
+ # are not provided
38
42
  #
39
43
  # Example:
40
44
  #
41
45
  # def do_it
42
- # Direct.defer{
46
+ # Direct.strict_defer{
43
47
  # [true, false].sample
44
48
  # }
45
49
  # end
46
50
  # do_it.
47
51
  # success{|result| puts "it worked!" }.
48
- # failure{|result| puts "it failed!" }
52
+ # failure{|result| puts "it failed!" }.
53
+ # value
54
+ #
55
+ def self.strict_defer(&block)
56
+ StrictExecutable.new(&block)
57
+ end
58
+
59
+ # Wrap a block of code to return an object for handling
60
+ # success or failure.
61
+ #
62
+ # Example:
63
+ #
64
+ # def do_it
65
+ # Direct.defer{
66
+ # [true, false].sample
67
+ # }
68
+ # end
69
+ # do_it.value
49
70
  #
50
71
  def self.defer(&block)
51
72
  Executable.new(&block)
@@ -1,6 +1,6 @@
1
1
  module Direct
2
2
  class Executable
3
- include Direct
3
+ include Direct.allow_missing_directions
4
4
 
5
5
  # It is intended that you initialize objects via Direct.defer
6
6
  # and not directly initializing this class.
@@ -0,0 +1,91 @@
1
+ module Direct
2
+ class StrictExecutable
3
+ include Direct
4
+
5
+ # It is intended that you initialize objects via Direct.strict_defer
6
+ # and not directly initializing this class.
7
+ #
8
+ # You may initialize this class and provide an object which
9
+ # responds to "call" or a block to execute.
10
+ #
11
+ # Example:
12
+ #
13
+ # Direct.strict_defer do
14
+ # puts "see ya later!"
15
+ # end
16
+ #
17
+ # Direct.strict_defer(->{ "call me, maybe" })
18
+ #
19
+ def initialize(callable=nil, &block)
20
+ @execution = callable || block
21
+ @exception_classes = [StandardError]
22
+ end
23
+ attr_reader :execution, :exception_classes
24
+
25
+ # Tell the object what to do for a success path
26
+ #
27
+ # Returns itself
28
+ #
29
+ def success(callable=nil, &block)
30
+ direct(:success, (callable || block))
31
+ self
32
+ end
33
+
34
+ # Tell the object what to do for a failure path
35
+ #
36
+ # Returns itself
37
+ #
38
+ def failure(callable=nil, &block)
39
+ direct(:failure, (callable || block))
40
+ self
41
+ end
42
+
43
+ # Tell the object what to do for an exception path.
44
+ #
45
+ # You may optionally provide a list of modules rescued by
46
+ # the value method in the case of an exception.
47
+ #
48
+ # Returns itself
49
+ #
50
+ # Example:
51
+ #
52
+ # Direct.strict_defer {
53
+ # # something...
54
+ # }.exception(NoMethodError) { |deferred, exception|
55
+ # ExceptionNotifier.notify(exception)
56
+ # }
57
+ #
58
+ def exception(*classes, &block)
59
+ unless classes.empty?
60
+ @exception_classes = classes
61
+ end
62
+ direct(:exception, block)
63
+ self
64
+ end
65
+
66
+ def run_exception_block(exception)
67
+ if __directions.key?(:exception)
68
+ as_directed(:exception, exception)
69
+ else
70
+ as_directed(:failure, exception)
71
+ end
72
+ end
73
+ private :run_exception_block
74
+
75
+ # Return the value of the success or failure path
76
+ # and rescue from StandardError or from the modules
77
+ # provided to the exception path.
78
+ #
79
+ def value
80
+ result = execution.()
81
+ if result
82
+ as_directed(:success, result)
83
+ else
84
+ as_directed(:failure, result)
85
+ end
86
+ rescue *exception_classes => exception
87
+ run_exception_block(exception)
88
+ end
89
+ alias execute value
90
+ end
91
+ end
@@ -1,3 +1,3 @@
1
1
  module Direct
2
- VERSION = "1.1.1"
2
+ VERSION = "1.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: direct
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jim Gay
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-02-21 00:00:00.000000000 Z
11
+ date: 2019-03-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -73,6 +73,7 @@ files:
73
73
  - lib/direct.rb
74
74
  - lib/direct/executable.rb
75
75
  - lib/direct/group.rb
76
+ - lib/direct/strict_executable.rb
76
77
  - lib/direct/version.rb
77
78
  homepage: https://github.com/saturnflyer/direct
78
79
  licenses: