Module: IIInteractor::Variables

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
lib/ii_interactor/variables.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.resolve(interactor, value) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/ii_interactor/variables.rb', line 45

def resolve(interactor, value)
  if value.respond_to?(:call)
    interactor.instance_exec(&value)
  elsif value.is_a?(Symbol) && interactor.respond_to?(value, true)
    interactor.send(value)
  else
    value.deep_dup
  end
end

Instance Method Details

#call_selfObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ii_interactor/variables.rb', line 21

def call_self
  (self.class.context_ins + self.class.context_outs).each do |var|
    if var.required? && !@context.respond_to?(var.name)
      raise RequiredContextError.new("missing required context: #{var.name}")
    end
    if var.default && !@context.respond_to?(var.name)
      @context[var.name] = Variables.resolve(self, var.default)
    end
    instance_variable_set("@#{var.name}", @context[var.name])
  end

  super.tap do |return_value|
    self.class.context_outs.each do |var|
      @context[var.name] =
          if var.from_return?
            return_value
          else
            instance_variable_get("@#{var.name}")
          end
    end
  end
end