Class: Direct::StrictExecutable
- Inherits:
-
Object
- Object
- Direct::StrictExecutable
- Includes:
- Direct
- Defined in:
- lib/direct/strict_executable.rb
Constant Summary
Constants included from Direct
Instance Attribute Summary collapse
-
#exception_classes ⇒ Object
readonly
Returns the value of attribute exception_classes.
-
#execution ⇒ Object
readonly
Returns the value of attribute execution.
Instance Method Summary collapse
-
#exception(*classes, &block) ⇒ Object
Tell the object what to do for an exception path.
-
#failure(callable = nil, &block) ⇒ Object
Tell the object what to do for a failure path.
-
#initialize(callable = nil, &block) ⇒ StrictExecutable
constructor
It is intended that you initialize objects via Direct.strict_defer and not directly initializing this class.
-
#success(callable = nil, &block) ⇒ Object
Tell the object what to do for a success path.
-
#value ⇒ Object
(also: #execute)
Return the value of the success or failure path and rescue from StandardError or from the modules provided to the exception path.
Methods included from Direct
allow_missing_directions, #as_directed, defer, #direct, strict_defer
Constructor Details
#initialize(callable = nil, &block) ⇒ StrictExecutable
It is intended that you initialize objects via Direct.strict_defer and not directly initializing this class.
You may initialize this class and provide an object which responds to “call” or a block to execute.
Example:
Direct.strict_defer do
puts "see ya later!"
end
Direct.strict_defer(->{ "call me, maybe" })
19 20 21 22 |
# File 'lib/direct/strict_executable.rb', line 19 def initialize(callable=nil, &block) @execution = callable || block @exception_classes = [StandardError] end |
Instance Attribute Details
#exception_classes ⇒ Object (readonly)
Returns the value of attribute exception_classes.
23 24 25 |
# File 'lib/direct/strict_executable.rb', line 23 def exception_classes @exception_classes end |
#execution ⇒ Object (readonly)
Returns the value of attribute execution.
23 24 25 |
# File 'lib/direct/strict_executable.rb', line 23 def execution @execution end |
Instance Method Details
#exception(*classes, &block) ⇒ Object
Tell the object what to do for an exception path.
You may optionally provide a list of modules rescued by the value method in the case of an exception.
Returns itself
Example:
Direct.strict_defer {
# something...
}.exception(NoMethodError) { |deferred, exception|
ExceptionNotifier.notify(exception)
}
58 59 60 61 62 63 64 |
# File 'lib/direct/strict_executable.rb', line 58 def exception(*classes, &block) unless classes.empty? @exception_classes = classes end direct(:exception, block) self end |
#failure(callable = nil, &block) ⇒ Object
Tell the object what to do for a failure path
Returns itself
38 39 40 41 |
# File 'lib/direct/strict_executable.rb', line 38 def failure(callable=nil, &block) direct(:failure, (callable || block)) self end |
#success(callable = nil, &block) ⇒ Object
Tell the object what to do for a success path
Returns itself
29 30 31 32 |
# File 'lib/direct/strict_executable.rb', line 29 def success(callable=nil, &block) direct(:success, (callable || block)) self end |
#value ⇒ Object Also known as: execute
Return the value of the success or failure path and rescue from StandardError or from the modules provided to the exception path.
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/direct/strict_executable.rb', line 79 def value result = execution.() if result as_directed(:success, result) else as_directed(:failure, result) end rescue *exception_classes => exception run_exception_block(exception) end |