class Direct::StrictExecutable

Attributes

exception_classes[R]
execution[R]
object[R]

Public Class Methods

new(callable=nil, object: nil, &block) click to toggle source

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" })

You may also provide an 'object:' parameter which will be passed to the provided blocks when they are ultimately executed

Example:

Direct.strict_defer(object: self) do
  # do work here
end.success {|deferred, deferred_block_result, object|
  # you have access to the provided object here
}

The object is a useful reference because the Executable instance is the deferred object, instead of the object using Direct.strict_defer

class Thing
  def do_something
    Direct.strict_defer(object: self){ do_the_work }
  end
end

Thing.new.do_something.success {|deferred, result, thing|
   puts "The #{thing} did something!"
}.failure{|_,_,thing|
   puts "#{thing} failed!"
}.execute
# File lib/direct/strict_executable.rb, line 45
def initialize(callable=nil, object: nil, &block)
  @object = object
  @execution = callable || block
  @exception_classes = [StandardError]
end

Public Instance Methods

exception(*classes, &block) click to toggle source

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, object|
   ExceptionNotifier.notify(exception)
}
# File lib/direct/strict_executable.rb, line 85
def exception(*classes, &block)
  unless classes.empty?
    @exception_classes = classes
  end
  direct(:exception, block)
  self
end
execute()
Alias for: value
failure(callable=nil, &block) click to toggle source

Tell the object what to do for a failure path

Returns itself

# File lib/direct/strict_executable.rb, line 65
def failure(callable=nil, &block)
  direct(:failure, (callable || block))
  self
end
success(callable=nil, &block) click to toggle source

Tell the object what to do for a success path

Returns itself

# File lib/direct/strict_executable.rb, line 56
def success(callable=nil, &block)
  direct(:success, (callable || block))
  self
end
value() click to toggle source

Return the value of the success or failure path and rescue from StandardError or from the modules provided to the exception path.

# File lib/direct/strict_executable.rb, line 106
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
Also aliased as: execute

Private Instance Methods

run_exception_block(exception) click to toggle source
# File lib/direct/strict_executable.rb, line 93
def run_exception_block(exception)
  if __directions.key?(:exception)
    as_directed(:exception, exception, object)
  else
    as_directed(:failure, exception, object)
  end
end