Class: ActiveInteractor::Context::Base

Inherits:
OpenStruct
  • Object
show all
Includes:
Attributes, Status, ActiveModel::Validations
Defined in:
lib/active_interactor/context/base.rb

Overview

The base context class. All interactor contexts should inherit from Base.

Author:

Since:

  • 0.0.1

Instance Method Summary collapse

Methods included from Status

#called!, #fail!, #failure?, #rollback!, #success?

Methods included from Attributes

#attributes, included

Constructor Details

#initialize(context = {}) ⇒ Context::Base

Returns a new instance of ActiveInteractor::Context::Base.

Parameters:

  • context (Hash|Context::Base) (defaults to: {})

    attributes to assign to the context

Since:

  • 0.0.1


21
22
23
24
25
26
# File 'lib/active_interactor/context/base.rb', line 21

def initialize(context = {})
  merge_errors!(context.errors) if context.respond_to?(:errors)
  copy_flags!(context)
  copy_called!(context)
  super
end

Instance Method Details

#merge!(context) ⇒ Base

Merge an instance of context or a hash into an existing context class MyInteractor2 < ActiveInteractor::Base def perform context.last_name = 'Allen' end end

result = MyInteractor1.perform #=> <#MyInteractor1::Context first_name='Aaron'>

result.merge!(MyInteractor2.perform) #=> <#MyInteractor1::Context first_name='Aaron' last_name='Allen'>

Examples:

class MyInteractor1 < ActiveInteractor::Base
  def perform
    context.first_name = 'Aaron'
  end
end

Parameters:

  • context (Base|Hash)

    attributes to merge into the context

Returns:

Since:

  • 1.0.0


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

def merge!(context)
  merge_errors!(context.errors) if context.respond_to?(:errors)
  copy_flags!(context)
  context.each_pair do |key, value|
    self[key] = value
  end
  self
end

#valid?(context = nil) ⇒ Object


# File 'lib/active_interactor/context/base.rb', line 28