Class: Direct::StrictExecutable

Inherits:
Object
  • Object
show all
Includes:
Direct
Defined in:
lib/direct/strict_executable.rb

Constant Summary

Constants included from Direct

VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Direct

allow_missing_directions, #as_directed, defer, #direct, strict_defer

Constructor Details

#initialize(callable = nil, object: 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" })

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


45
46
47
48
49
# File 'lib/direct/strict_executable.rb', line 45

def initialize(callable=nil, object: nil, &block)
  @object = object
  @execution = callable || block
  @exception_classes = [StandardError]
end

Instance Attribute Details

#exception_classesObject (readonly)

Returns the value of attribute exception_classes.



50
51
52
# File 'lib/direct/strict_executable.rb', line 50

def exception_classes
  @exception_classes
end

#executionObject (readonly)

Returns the value of attribute execution.



50
51
52
# File 'lib/direct/strict_executable.rb', line 50

def execution
  @execution
end

#objectObject (readonly)

Returns the value of attribute object.



50
51
52
# File 'lib/direct/strict_executable.rb', line 50

def object
  @object
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, object|
   ExceptionNotifier.notify(exception)
}


85
86
87
88
89
90
91
# File 'lib/direct/strict_executable.rb', line 85

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



65
66
67
68
# File 'lib/direct/strict_executable.rb', line 65

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



56
57
58
59
# File 'lib/direct/strict_executable.rb', line 56

def success(callable=nil, &block)
  direct(:success, (callable || block))
  self
end

#valueObject 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.



106
107
108
109
110
111
112
113
114
115
# 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