Class: Interactors::Simple

Inherits:
Object
  • Object
show all
Includes:
FunctionalInteractor
Defined in:
lib/interactors/simple.rb

Overview

Anonymous interactors Does two things:

- passes context to the proc
- honors #compose interface
- Only return [:error, ... ] if exception is raised
- otherwise, always return [:ok, context]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FunctionalInteractor

#compose

Constructor Details

#initialize(&blk) ⇒ Simple

Returns a new instance of Simple.



14
15
16
# File 'lib/interactors/simple.rb', line 14

def initialize(&blk)
  @block = blk
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



12
13
14
# File 'lib/interactors/simple.rb', line 12

def block
  @block
end

Instance Method Details

#call(context = {}) ⇒ Object



18
19
20
21
22
# File 'lib/interactors/simple.rb', line 18

def call(context = {})
  handle_error do
    block.call(context)
  end
end

#handle_errorObject

This is meant to be overridable. Maybe you want to use something else, such as capturing network errors, or wrap around specific third-party libraries.



28
29
30
31
32
33
# File 'lib/interactors/simple.rb', line 28

def handle_error
  yield
  [:ok, context]
rescue Exception => e
  [:error, e]
end