module ActiveInteractor::Context::Loader

Find or create {Base context} classes for a given {ActiveInteractor::Base interactor}

@api private @author Aaron Allen <hello@aaronmallen> @since 1.0.0

Constants

BASE_CLASSES

ActiveInteractor base classes @return [Array<Const>]

BASE_CONTEXT

The base {Base context} class all {Base context} objects should inherit from.

@author Aaron Allen <hello@aaronmallen.me> @since 0.1.0

@!method attribute(name, type=Type::Value.new, **options)

@!scope class
@since 1.0.1

@example Setting default values on the {Base context} class
  class MyContext < ActiveInteractor::Context::Base
    attribute :first_name, default: -> { 'Aaron' }
  end

  MyContext.new
  #=> <#MyContext first_name='Aaron'>

  MyContext.new(first_name: 'Bob')
  #=> <#MyContext first_name='Bob'>

@see
 https://api.rubyonrails.org/classes/ActiveModel/Attributes/ClassMethods.html#method-i-attribute
 ActiveModel::Attributes::ClassMethods#attribute

@!method attribute_method?(attribute)

@!scope class
@since 0.1.0
@see
 https://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html#method-i-attribute_method-3F
 ActiveModel::Validations::ClassMethods#attribute_method?

@!method attribute_missing(match, *args, &block)

@!scope class
@since 1.0.1
@see
 https://api.rubyonrails.org/classes/ActiveModel/AttributeMethods.html#method-i-attribute_missing
 ActiveModel::AttributeMethods#attribute_missing

@!method attribute_names

@!scope class
@since 1.0.1
@see
 https://api.rubyonrails.org/classes/ActiveModel/Attributes/ClassMethods.html#method-i-attribute_names
 ActiveModel::Attributes::ClassMethods#attribute_names

@!method clear_validators!(attribute)

@!scope class
@since 0.1.0
@see
 https://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html#method-i-clear_validators-21
 ActiveModel::Validations::ClassMethods#clear_validators!

@!method method_missing(method, *args, &block)

@scope class
@since 1.0.1
@see
 https://api.rubyonrails.org/classes/ActiveModel/AttributeMethods.html#method-i-method_missing
 ActiveModel::AttributeMethods#method_missing

@!method respond_to?(method, include_private_methods = false)

@scope class
@since 1.0.1
@see
 https://api.rubyonrails.org/classes/ActiveModel/AttributeMethods.html#method-i-respond_to-3F
 ActiveModel::AttributeMethods#respond_to?

@!method respond_to_without_attributes?(method, include_private_methods = false)

@scope class
@since 1.0.1
@see
 https://api.rubyonrails.org/classes/ActiveModel/AttributeMethods.html#method-i-respond_to_without_attributes-3F
 ActiveModel::AttributeMethods#respond_to_without_attributes?

@!method validate(*args, &block)

@!scope class
@since 0.1.0
@see
 https://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html#method-i-validate
 ActiveModel::Validations::ClassMethods#validate

@!method validates(*attributes)

@!scope class
@since 0.1.0
@see
 https://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html#method-i-validates
 ActiveModel::Validations::ClassMethods#validates

@!method validates!(*attributes)

@!scope class
@since 0.1.0
@see
 https://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html#method-i-validates-21
 ActiveModel::Validations::ClassMethods#validates!

@!method validates_absence_of(*attr_names)

@!scope class
@since 0.1.0
@see
 https://api.rubyonrails.org/classes/ActiveModel/Validations/HelperMethods.html#method-i-validates_absence_of
 ActiveModel::Validations::HelperMethods#validates_absence_of

@!method validates_acceptance_of(*attr_names)

@!scope class
@since 0.1.0
@see
 https://api.rubyonrails.org/classes/ActiveModel/Validations/HelperMethods.html#method-i-validates_acceptance_of
 ActiveModel::Validations::HelperMethods#validates_acceptance_of

@!method validates_confirmation_of(*attr_names)

@!scope class
@since 0.1.0
@see
 https://api.rubyonrails.org/classes/ActiveModel/Validations/HelperMethods.html#method-i-validates_confirmation_of
 ActiveModel::Validations::HelperMethods#validates_confirmation_of

@!method validates_each(*attr_names, &block)

@!scope class
@since 0.1.0
@see
 https://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html#method-i-validates_each
 ActiveModel::Validations::ClassMethods#validates_each

@!method validates_exclusion_of(*attr_names)

@!scope class
@since 0.1.0
@see
 https://api.rubyonrails.org/classes/ActiveModel/Validations/HelperMethods.html#method-i-validates_exclusion_of
 ActiveModel::Validations::HelperMethods#validates_exclusion_of

@!method validates_format_of(*attr_names)

@!scope class
@since 0.1.0
@see
 https://api.rubyonrails.org/classes/ActiveModel/Validations/HelperMethods.html#method-i-validates_format_of
 ActiveModel::Validations::HelperMethods#validates_format_of

@!method validates_inclusion_of(*attr_names)

@!scope class
@since 0.1.0
@see
 https://api.rubyonrails.org/classes/ActiveModel/Validations/HelperMethods.html#method-i-validates_inclusion_of
 ActiveModel::Validations::HelperMethods#validates_inclusion_of

@!method validates_length_of(*attr_names)

@!scope class
@since 0.1.0
@see
 https://api.rubyonrails.org/classes/ActiveModel/Validations/HelperMethods.html#method-i-validates_length_of
 ActiveModel::Validations::HelperMethods#validates_length_of

@!method validates_numericality_of(*attr_names)

@!scope class
@since 0.1.0
@see
 https://api.rubyonrails.org/classes/ActiveModel/Validations/HelperMethods.html#method-i-validates_numericality_of
 ActiveModel::Validations::HelperMethods#validates_numericality_of

@!method validates_presence_of(*attr_names)

@!scope class
@since 0.1.0
@see
 https://api.rubyonrails.org/classes/ActiveModel/Validations/HelperMethods.html#method-i-validates_presence_of
 ActiveModel::Validations::HelperMethods#validates_presence_of

@!method validates_size_of(*attr_names)

@!scope class
@since 0.1.0
@see
 https://api.rubyonrails.org/classes/ActiveModel/Validations/HelperMethods.html#method-i-validates_size_of
 ActiveModel::Validations::HelperMethods#validates_size_of

@!method validates_with(*args, &block)

@!scope class
@since 0.1.0
@see
 https://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html#method-i-validates_with
 ActiveModel::Validations::ClassMethods#validates_with

@!method validators

@!scope class
@since 0.1.0
@see
 https://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html#method-i-validators
 ActiveModel::Validations::ClassMethods#validators

@!method validators_on(*attributes)

@!scope class
@since 0.1.0
@see
 https://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html#method-i-validators_on
 ActiveModel::Validations::ClassMethods#validators_on

@!method attribute_missing(match, *args, &block)

@since 1.0.1
@see
 https://api.rubyonrails.org/classes/ActiveModel/AttributeMethods.html#method-i-attribute_missing
 ActiveModel::AttributeMethods#attribute_missing

@!method attribute_names

@since 1.0.1
@see
 https://api.rubyonrails.org/classes/ActiveModel/Attributes/ClassMethods.html#method-i-attribute_names
 ActiveModel::Attributes::ClassMethods#attribute_names

@!method errors

@since 0.1.0
@see
 https://api.rubyonrails.org/classes/ActiveModel/Validations.html#method-i-errors
 ActiveModel::Validations#errors

@!method invalid?(context = nil)

@since 0.1.0
@see
 https://api.rubyonrails.org/classes/ActiveModel/Validations.html#method-i-invalid-3F
 ActiveModel::Validations#invalid?

@!method method_missing(method, *args, &block)

@since 1.0.1
@see
 https://api.rubyonrails.org/classes/ActiveModel/AttributeMethods.html#method-i-method_missing
 ActiveModel::AttributeMethods#method_missing

@!method respond_to?(method, include_private_methods = false)

@since 1.0.1
@see
 https://api.rubyonrails.org/classes/ActiveModel/AttributeMethods.html#method-i-respond_to-3F
 ActiveModel::AttributeMethods#respond_to?

@!method respond_to_without_attributes?(method, include_private_methods = false)

@since 1.0.1
@see
 https://api.rubyonrails.org/classes/ActiveModel/AttributeMethods.html#method-i-respond_to_without_attributes-3F
 ActiveModel::AttributeMethods#respond_to_without_attributes?

@!method valid?(context = nil)

@since 0.1.0
@see
 https://api.rubyonrails.org/classes/ActiveModel/Validations.html#method-i-valid-3F
 ActiveModel::Validations#valid?

@!method validate(context = nil)

@since 0.1.0
@see
 https://api.rubyonrails.org/classes/ActiveModel/Validations.html#method-i-validate
 ActiveModel::Validations#validate

@!method validate!(context = nil)

@since 0.1.0
@see
 https://api.rubyonrails.org/classes/ActiveModel/Validations.html#method-i-validate-21
 ActiveModel::Validations#validate!

@!method validates_absence_of(*attr_names)

@since 0.1.0
@see
 https://api.rubyonrails.org/classes/ActiveModel/Validations/HelperMethods.html#method-i-validates_absence_of
 ActiveModel::Validations::HelperMethods#validates_absence_of

@!method validates_acceptance_of(*attr_names)

@since 0.1.0
@see
 https://api.rubyonrails.org/classes/ActiveModel/Validations/HelperMethods.html#method-i-validates_acceptance_of
 ActiveModel::Validations::HelperMethods#validates_acceptance_of

@!method validates_confirmation_of(*attr_names)

@since 0.1.0
@see
 https://api.rubyonrails.org/classes/ActiveModel/Validations/HelperMethods.html#method-i-validates_confirmation_of
 ActiveModel::Validations::HelperMethods#validates_confirmation_of

@!method validates_exclusion_of(*attr_names)

@since 0.1.0
@see
 https://api.rubyonrails.org/classes/ActiveModel/Validations/HelperMethods.html#method-i-validates_exclusion_of
 ActiveModel::Validations::HelperMethods#validates_exclusion_of

@!method validates_format_of(*attr_names)

@since 0.1.0
@see
 https://api.rubyonrails.org/classes/ActiveModel/Validations/HelperMethods.html#method-i-validates_format_of
 ActiveModel::Validations::HelperMethods#validates_format_of

@!method validates_inclusion_of(*attr_names)

@since 0.1.0
@see
 https://api.rubyonrails.org/classes/ActiveModel/Validations/HelperMethods.html#method-i-validates_inclusion_of
 ActiveModel::Validations::HelperMethods#validates_inclusion_of

@!method validates_length_of(*attr_names)

@since 0.1.0
@see
 https://api.rubyonrails.org/classes/ActiveModel/Validations/HelperMethods.html#method-i-validates_length_of
 ActiveModel::Validations::HelperMethods#validates_length_of

@!method validates_numericality_of(*attr_names)

@since 0.1.0
@see
 https://api.rubyonrails.org/classes/ActiveModel/Validations/HelperMethods.html#method-i-validates_numericality_of
 ActiveModel::Validations::HelperMethods#validates_numericality_of

@!method validates_presence_of(*attr_names)

@since 0.1.0
@see
 https://api.rubyonrails.org/classes/ActiveModel/Validations/HelperMethods.html#method-i-validates_presence_of
 ActiveModel::Validations::HelperMethods#validates_presence_of

@!method validates_size_of(*attr_names)

@since 0.1.0
@see
 https://api.rubyonrails.org/classes/ActiveModel/Validations/HelperMethods.html#method-i-validates_size_of
 ActiveModel::Validations::HelperMethods#validates_size_of

@!method validates_with(*args, &block)

@since 0.1.0
@see
 https://api.rubyonrails.org/classes/ActiveModel/Validations.html#method-i-validates_with
 ActiveModel::Validations#validates_with

Public Class Methods

create(context_class_name, interactor_class) click to toggle source

Create a {Base context} class for a given {ActiveInteractor::Base interactor}

@param context_class_name [Symbol, String] the class name of the

{Base context} class to create

@param interactor_class [Const] an {ActiveInteractor::Base interactor} class @return [Const] a class that inherits from {Base}

# File lib/active_interactor/context/loader.rb, line 24
def self.create(context_class_name, interactor_class)
  interactor_class.const_set(context_class_name.to_s.camelize, Class.new(BASE_CONTEXT))
end
find_or_create(interactor_class) click to toggle source

Find or create a {Base context} class for a given {ActiveInteractor::Base interactor}. If a class exists following the pattern of `InteractorNameContext` or `InteractorName::Context` then that class will be returned otherwise a new class will be created with the pattern `InteractorName::Context`.

@param interactor_class [Const] an {ActiveInteractor::Base interactor} class @return [Const] a class that inherits from {Base}

# File lib/active_interactor/context/loader.rb, line 34
def self.find_or_create(interactor_class)
  return BASE_CONTEXT if BASE_CLASSES.include?(interactor_class)

  klass = possible_classes(interactor_class).first
  klass || create('Context', interactor_class)
end

Private Class Methods

possible_classes(interactor_class) click to toggle source
# File lib/active_interactor/context/loader.rb, line 41
def self.possible_classes(interactor_class)
  ["#{interactor_class.name}::Context", "#{interactor_class.name}Context"]
    .map(&:safe_constantize)
    .compact
    .reject { |klass| klass&.name&.include?('ActiveInteractor') }
end