class ActiveInteraction::Base

@abstract Subclass and override {#execute} to implement a custom

ActiveInteraction::Base class.

Provides interaction functionality. Subclass this to create an interaction.

@example

class ExampleInteraction < ActiveInteraction::Base
  # Required
  boolean :a

  # Optional
  boolean :b, default: false

  def execute
    a && b
  end
end

outcome = ExampleInteraction.run(a: true)
if outcome.valid?
  outcome.result
else
  outcome.errors
end

Public Class Methods

desc(desc = nil) click to toggle source

Get or set the description.

@example

core.desc
# => nil
core.desc('Description!')
core.desc
# => "Description!"

@param desc [String, nil] What to set the description to.

@return [String, nil] The description.

# File lib/active_interaction/base.rb, line 72
def desc(desc = nil)
  if desc.nil?
    @_interaction_desc = nil unless instance_variable_defined?(:@_interaction_desc)
  else
    @_interaction_desc = desc
  end

  @_interaction_desc
end
filters() click to toggle source

Get all the filters defined on this interaction.

@return [Hash{Symbol => Filter}]

# File lib/active_interaction/base.rb, line 85
def filters
  # rubocop:disable Naming/MemoizedInstanceVariableName
  @_interaction_filters ||= {}
  # rubocop:enable Naming/MemoizedInstanceVariableName
end
new(inputs = {}) click to toggle source

@private

# File lib/active_interaction/base.rb, line 163
def initialize(inputs = {})
  @_interaction_raw_inputs = inputs

  @_interaction_inputs = Inputs.new(inputs, self) do |name, input|
    public_send("#{name}=", input.value)
  end
end

Private Class Methods

add_filter(klass, name, options, &block) click to toggle source

@param klass [Class] @param name [Symbol] @param options [Hash]

# File lib/active_interaction/base.rb, line 106
def add_filter(klass, name, options, &block)
  raise InvalidFilterError, %("#{name}" is a reserved name) if Inputs.reserved?(name)

  initialize_filter(klass.new(name, options, &block))
end
eagerly_evaluate_default(filter) click to toggle source

@param filter [Filter]

# File lib/active_interaction/base.rb, line 156
def eagerly_evaluate_default(filter)
  default = filter.options[:default]
  filter.default if default && !default.is_a?(Proc)
end
import_filters(klass, options = {}) click to toggle source

Import filters from another interaction.

@param klass [Class] The other interaction. @param options [Hash]

@option options [Array<Symbol>, nil] :only Import only these filters. @option options [Array<Symbol>, nil] :except Import all filters except

for these.

@return (see .filters)

@!visibility public

# File lib/active_interaction/base.rb, line 124
def import_filters(klass, options = {})
  only = options[:only]
  except = options[:except]

  other_filters = klass.filters.dup
  other_filters.select! { |k, _| [*only].include?(k) } if only
  other_filters.reject! { |k, _| [*except].include?(k) } if except

  other_filters.each_value { |filter| initialize_filter(filter) }
end
inherited(klass) click to toggle source

@param klass [Class]

Calls superclass method
# File lib/active_interaction/base.rb, line 136
def inherited(klass)
  klass.instance_variable_set(:@_interaction_filters, filters.dup)

  super
end
initialize_filter(filter) click to toggle source

@param filter [Filter]

# File lib/active_interaction/base.rb, line 143
def initialize_filter(filter)
  attribute = filter.name
  warn "WARNING: Redefining #{name}##{attribute} filter" if filters.key?(attribute)
  filters[attribute] = filter

  attr_accessor attribute

  alias_method "#{attribute}?", attribute if filter.is_a?(BooleanFilter)

  eagerly_evaluate_default(filter)
end
method_missing(*args, &block) click to toggle source

rubocop:disable Style/MissingRespondToMissing

# File lib/active_interaction/base.rb, line 94
def method_missing(*args, &block)
  super do |klass, names, options|
    raise InvalidFilterError, 'missing attribute name' if names.empty?

    names.each { |name| add_filter(klass, name, options, &block) }
  end
end

Public Instance Methods

inputs() click to toggle source

Returns the inputs provided to {.run} or {.run!} after being cast based

on the filters in the class.

@return [Inputs] All expected inputs passed to {.run} or {.run!}.

# File lib/active_interaction/base.rb, line 193
def inputs
  @_interaction_inputs
end
read_attribute_for_validation(attribute) click to toggle source

@private

Calls superclass method
# File lib/active_interaction/base.rb, line 198
def read_attribute_for_validation(attribute)
  super(errors.local_attribute(attribute))
end

Protected Instance Methods

run_validations!() click to toggle source
Calls superclass method
# File lib/active_interaction/base.rb, line 204
def run_validations!
  filter

  super if errors.empty?
end

Private Instance Methods

filter() click to toggle source
# File lib/active_interaction/base.rb, line 212
def filter
  run_callbacks(:filter) do
    Validation.validate(self, self.class.filters, inputs).each do |attr, type, kwargs = {}|
      errors.add(attr, type, **kwargs)
    end
  end
end