Class: ActiveInteractor::Base

Inherits:
Object
  • Object
show all
Includes:
Type::HasTypes
Defined in:
lib/active_interactor/base.rb

Constant Summary

Constants included from Type::HasTypes

Type::HasTypes::Boolean

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input = {}) ⇒ Base

Returns a new instance of Base.



51
52
53
54
# File 'lib/active_interactor/base.rb', line 51

def initialize(input = {})
  @raw_input = input.dup
  validate_input_and_generate_runtime_context!
end

Class Method Details

.accepts_arguments_matching(set_input_context_class) ⇒ Object Also known as: input_context



15
16
17
# File 'lib/active_interactor/base.rb', line 15

def accepts_arguments_matching(set_input_context_class)
  @input_context_class = set_input_context_class
end

.input_context_classObject



11
12
13
# File 'lib/active_interactor/base.rb', line 11

def input_context_class
  @input_context_class ||= const_set(:InputContext, Class.new(Context::Input))
end

.output_context_classObject



20
21
22
# File 'lib/active_interactor/base.rb', line 20

def output_context_class
  @output_context_class ||= const_set(:OutputContext, Class.new(Context::Output))
end

.perform(input_context = {}) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/active_interactor/base.rb', line 28

def perform(input_context = {})
  perform!(input_context)
rescue Error => e
  e.result
rescue StandardError => e
  Result.failure(errors: e.message)
end

.perform!(input_context = {}) ⇒ Object



24
25
26
# File 'lib/active_interactor/base.rb', line 24

def perform!(input_context = {})
  new(input_context).perform!
end

.returns_data_matching(set_output_context_class) ⇒ Object Also known as: output_context



36
37
38
# File 'lib/active_interactor/base.rb', line 36

def returns_data_matching(set_output_context_class)
  @output_context_class = set_output_context_class
end

.runtime_context_classObject



41
42
43
44
45
46
47
48
# File 'lib/active_interactor/base.rb', line 41

def runtime_context_class
  @runtime_context_class ||= begin
    context_class = const_set(:RuntimeContext, Class.new(Context::Runtime))
    context_class.send(:attribute_set).merge(input_context_class.send(:attribute_set).attributes)
    context_class.send(:attribute_set).merge(output_context_class.send(:attribute_set).attributes)
    context_class
  end
end

Instance Method Details

#interactObject



72
# File 'lib/active_interactor/base.rb', line 72

def interact; end

#performObject



64
65
66
67
68
69
70
# File 'lib/active_interactor/base.rb', line 64

def perform
  perform!
rescue Error => e
  e.result
rescue StandardError => e
  Result.failure(errors: e.message)
end

#perform!Object



56
57
58
59
60
61
62
# File 'lib/active_interactor/base.rb', line 56

def perform!
  with_notification(:perform) do |payload|
    interact
    generate_and_validate_output_context!
    payload[:result] = Result.success(data: @output)
  end
end

#rollbackObject



73
# File 'lib/active_interactor/base.rb', line 73

def rollback; end