Class: Direct::Executable

Inherits:
Object
  • Object
show all
Defined in:
lib/direct/executable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(callable = nil, object: nil, &block) ⇒ Executable

It is intended that you initialize objects via Direct.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.defer do
  puts "see ya later!"
end

Direct.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.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.defer

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

Thing.new.do_something.success {|deferred, result, thing|
   puts "The #{thing} did something!"
}.execute


43
44
45
46
47
# File 'lib/direct/executable.rb', line 43

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.



48
49
50
# File 'lib/direct/executable.rb', line 48

def exception_classes
  @exception_classes
end

#executionObject (readonly)

Returns the value of attribute execution.



48
49
50
# File 'lib/direct/executable.rb', line 48

def execution
  @execution
end

#objectObject (readonly)

Returns the value of attribute object.



48
49
50
# File 'lib/direct/executable.rb', line 48

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.defer {
   # something...
}.exception(NoMethodError) { |deferred, exception, object|
   ExceptionNotifier.notify(exception)
}


83
84
85
86
87
88
89
# File 'lib/direct/executable.rb', line 83

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



63
64
65
66
# File 'lib/direct/executable.rb', line 63

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



54
55
56
57
# File 'lib/direct/executable.rb', line 54

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.



104
105
106
107
108
109
110
111
112
113
# File 'lib/direct/executable.rb', line 104

def value
  result = execution.()
  if result
    as_directed(:success, result, object)
  else
    as_directed(:failure, result, object)
  end
rescue *exception_classes => exception
  run_exception_block(exception)
end