Module: ActiveInteractor::Interactor::Context

Defined in:
lib/active_interactor/interactor/context.rb

Overview

Interactor context methods included by all Base

Author:

Since:

  • 0.0.1

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#contextContext::Base

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns an instance of the interactor's context class.

Returns:

  • (Context::Base)

    an instance of the interactor's context class


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/active_interactor/interactor/context.rb', line 14

module Context
  # @!method context_fail!(errors = nil)
  # Fail the interactor's context
  # @since 1.0.0
  # @see ActiveInteractor::Context::Base#fail!

  # @!method context_rollback!
  # Rollback the interactor's context
  # @since 1.0.0
  # @see ActiveInteractor::Context::Base#rollback!

  # @!method context_valid?(context = nil)
  # Whether or not the interactor's context is valid
  # @since 1.0.0
  # @see ActiveInteractor::Context::Base#valid?
  def self.included(base)
    base.class_eval do
      extend ClassMethods
      delegate :fail!, :rollback!, to: :context, prefix: true
      delegate(*ActiveModel::Validations.instance_methods, to: :context, prefix: true)
      delegate(*ActiveModel::Validations::HelperMethods.instance_methods, to: :context, prefix: true)

      private

      attr_accessor :context
    end
  end

  # Interactor context class methods extended by all {Base}
  module ClassMethods
    delegate(*ActiveModel::Validations::ClassMethods.instance_methods, to: :context_class, prefix: :context)
    delegate(*ActiveModel::Validations::HelperMethods.instance_methods, to: :context_class, prefix: :context)
    # @!method context_attributes(*attributes)
    # Set or get attributes defined on the interactor's context class
    # @since 0.0.1
    # @see ActiveInteractor::Context::Attributes#attributes
    delegate :attributes, to: :context_class, prefix: :context

    # The context class of the interactor
    # @note If a context class is not defined on the interactor
    #  with {#contextualize_with} ActiveInteractor will attempt
    #  to find a class with the naming conventions:
    #    `MyInteractor::Context` or `MyInteractorContext`
    #  if no matching context class is found a context class
    #  will be created with the naming convention: `MyInteractor::Context`.
    # @example With an existing class named `MyInteractor::Context`
    #  class MyInteractor::Context < ActiveInteractor::Context::Base
    #  end
    #
    #  class MyInteractor < ActiveInteractor::Base
    #  end
    #
    #  MyInteractor.context_class
    #  #=> MyInteractor::Context
    # @example With an existing class named `MyInteractorContext`
    #  class MyInteractorContext < ActiveInteractor::Context::Base
    #  end
    #
    #  class MyInteractor < ActiveInteractor::Base
    #  end
    #
    #  MyInteractor.context_class
    #  #=> MyInteractorContext
    # @example With no existing context class
    #  class MyInteractor < ActiveInteractor::Base
    #  end
    #
    #  MyInteractor.context_class
    #  #=> MyInteractor::Context
    # @return [Class] the interactor's context class
    def context_class
      @context_class ||= ActiveInteractor::Context::Loader.find_or_create(self)
    end

    # Manual define an interactor's context class
    # @since 1.0.0
    # @example
    #   class AGenericContext < ActiveInteractor::Context::Base
    #   end
    #
    #   class MyInteractor < ActiveInteractor::Base
    #     contextualize_with :a_generic_context
    #   end
    #
    #   MyInteractor.context_class
    #   #=> AGenericContext
    # @param klass [String|Symbol|Class] the context class
    # @raise [Error::InvalidContextClass] if no matching class can be found
    # @return [Class] the context class
    def contextualize_with(klass)
      @context_class = begin
        context_class = klass.to_s.classify.safe_constantize
        raise(Error::InvalidContextClass, klass) unless context_class

        context_class
      end
    end
  end

  # @api private
  # @param context [Context::Base|Hash] attributes to assign to the context
  # @return [Base] a new instane of {Base}
  def initialize(context = {})
    @context = self.class.context_class.new(context)
  end

  # @api private
  # Mark the interactor's context as called and return the context
  # @since 1.0.0
  # @return [Context::Base] an instance of the interactor's context class
  def finalize_context!
    context.called!(self)
    context
  end
end

Class Method Details

.included(base) ⇒ Object

Whether or not the interactor's context is valid

See Also:

Since:

  • 1.0.0


29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/active_interactor/interactor/context.rb', line 29

def self.included(base)
  base.class_eval do
    extend ClassMethods
    delegate :fail!, :rollback!, to: :context, prefix: true
    delegate(*ActiveModel::Validations.instance_methods, to: :context, prefix: true)
    delegate(*ActiveModel::Validations::HelperMethods.instance_methods, to: :context, prefix: true)

    private

    attr_accessor :context
  end
end

Instance Method Details

#context_fail!(errors = nil) ⇒ Object

Fail the interactor's context

See Also:

  • Context::Base#fail!

Since:

  • 1.0.0


# File 'lib/active_interactor/interactor/context.rb', line 15

#context_rollback!Object

Rollback the interactor's context

See Also:

  • Context::Base#rollback!

Since:

  • 1.0.0


# File 'lib/active_interactor/interactor/context.rb', line 20

#context_valid?(context = nil) ⇒ Object

Whether or not the interactor's context is valid

See Also:

Since:

  • 1.0.0


29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/active_interactor/interactor/context.rb', line 29

def self.included(base)
  base.class_eval do
    extend ClassMethods
    delegate :fail!, :rollback!, to: :context, prefix: true
    delegate(*ActiveModel::Validations.instance_methods, to: :context, prefix: true)
    delegate(*ActiveModel::Validations::HelperMethods.instance_methods, to: :context, prefix: true)

    private

    attr_accessor :context
  end
end

#finalize_context!Context::Base

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Mark the interactor's context as called and return the context

Returns:

  • (Context::Base)

    an instance of the interactor's context class

Since:

  • 1.0.0


124
125
126
127
# File 'lib/active_interactor/interactor/context.rb', line 124

def finalize_context!
  context.called!(self)
  context
end

#initialize(context = {}) ⇒ Base

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instane of Base.

Parameters:

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

    attributes to assign to the context

Returns:

Since:

  • 0.0.1


116
117
118
# File 'lib/active_interactor/interactor/context.rb', line 116

def initialize(context = {})
  @context = self.class.context_class.new(context)
end