Class: ActiveInteraction::Base Abstract

Inherits:
Object
  • Object
show all
Extended by:
Hashable, Missable
Includes:
ActiveModelable, Runnable
Defined in:
lib/active_interaction/base.rb,
lib/active_interaction/filters/date_filter.rb,
lib/active_interaction/filters/file_filter.rb,
lib/active_interaction/filters/hash_filter.rb,
lib/active_interaction/filters/time_filter.rb,
lib/active_interaction/filters/array_filter.rb,
lib/active_interaction/filters/float_filter.rb,
lib/active_interaction/filters/model_filter.rb,
lib/active_interaction/filters/string_filter.rb,
lib/active_interaction/filters/symbol_filter.rb,
lib/active_interaction/filters/boolean_filter.rb,
lib/active_interaction/filters/decimal_filter.rb,
lib/active_interaction/filters/integer_filter.rb,
lib/active_interaction/filters/date_time_filter.rb,
lib/active_interaction/filters/interface_filter.rb

Overview

This class is abstract.

Subclass and override #execute to implement a custom ActiveInteraction::Base class.

Provides interaction functionality. Subclass this to create an interaction.

Examples:

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

Since:

  • 1.0.0

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Missable

method_missing

Methods included from Hashable

hash

Methods included from Runnable

#errors, #result, #result=, #valid?

Methods included from Transactable

#transaction

Methods included from ActiveModelable

#i18n_scope, #new_record?, #persisted?

Constructor Details

#initialize(inputs = {}) ⇒ Base

Returns a new instance of Base.

Parameters:

  • inputs (Hash{Symbol => Object}) (defaults to: {})

    Attribute values to set.

Since:

  • 1.0.0



178
179
180
181
182
# File 'lib/active_interaction/base.rb', line 178

def initialize(inputs = {})
  fail ArgumentError, 'inputs must be a hash' unless inputs.is_a?(Hash)

  process_inputs(inputs.symbolize_keys)
end

Class Method Details

.desc(desc = nil) ⇒ String?

Get or set the description.

Examples:

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

Parameters:

  • desc (String, nil) (defaults to: nil)

    What to set the description to.

Returns:

  • (String, nil)

    The description.

Since:

  • 1.0.0



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/active_interaction/base.rb', line 95

def desc(desc = nil)
  if desc.nil?
    unless instance_variable_defined?(:@_interaction_desc)
      @_interaction_desc = nil
    end
  else
    @_interaction_desc = desc
  end

  @_interaction_desc
end

.filtersHash{Symbol => Filter}

Get all the filters defined on this interaction.

Returns:

Since:

  • 1.0.0



110
111
112
# File 'lib/active_interaction/base.rb', line 110

def filters
  @_interaction_filters ||= {}
end

.import_filters(klass, options = {}) ⇒ Hash{Symbol => Filter}

Import filters from another interaction.

Parameters:

  • klass (Class)

    The other interaction.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :only (Array<Symbol>, nil)

    Import only these filters.

  • :except (Array<Symbol>, nil)

    Import all filters except for these.

Returns:

Since:

  • 1.0.0



146
147
148
149
150
151
152
153
154
155
# File 'lib/active_interaction/base.rb', line 146

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.values.each { |filter| initialize_filter(filter) }
end

.method_missing(*args, &block) ⇒ Object

Since:

  • 1.0.0



115
116
117
118
119
120
121
# File 'lib/active_interaction/base.rb', line 115

def method_missing(*args, &block)
  super do |klass, names, options|
    fail InvalidFilterError, 'missing attribute name' if names.empty?

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

.run(inputs = {}) ⇒ Base

Note:

If the interaction inputs are valid and there are no runtime errors and execution completed successfully, Runnable#valid? will always return true.

Runs validations and if there are no errors it will call #execute.

Parameters:

  • inputs (Hash{Symbol => Object}) (defaults to: {})

    Attribute values to set.

Returns:



# File 'lib/active_interaction/base.rb', line 40

.run!(inputs = {}) ⇒ Object

Like run except that it returns the value of #execute or raises

an exception if there were any validation errors.

Parameters:

  • inputs (Hash{Symbol => Object}) (defaults to: {})

    Attribute values to set.

Returns:

  • (Object)

Raises:



# File 'lib/active_interaction/base.rb', line 51

.transaction(enable, options = {}) ⇒ nil

Configure transactions by enabling or disabling them and setting their options.

Examples:

Disable transactions

Class.new(ActiveInteraction::Base) do
  transaction false
end

Use different transaction options

Class.new(ActiveInteraction::Base) do
  transaction true, isolation: :serializable
end

Parameters:

  • enable (Boolean)

    Should transactions be enabled?

  • options (Hash) (defaults to: {})

    Options to pass to ActiveRecord::Base.transaction.

Returns:

  • (nil)

Since:

  • 1.2.0



# File 'lib/active_interaction/base.rb', line 61

Instance Method Details

#column_for_attribute(name) ⇒ FilterColumn?

Returns the column object for the named filter.

Examples:

class Interaction < ActiveInteraction::Base
  string :email, default: nil

  def execute; end
end

Interaction.new.column_for_attribute(:email)
# => #<ActiveInteraction::FilterColumn:0x007faebeb2a6c8 @type=:string>

Interaction.new.column_for_attribute(:not_a_filter)
# => nil

Parameters:

  • name (Symbol)

    The name of a filter.

Returns:

Since:

  • 1.2.0



204
205
206
207
# File 'lib/active_interaction/base.rb', line 204

def column_for_attribute(name)
  filter = self.class.filters[name]
  FilterColumn.intern(filter.database_column_type) if filter
end

#compose(other, inputs = {}) ⇒ Object

Run another interaction and return its result. If the other interaction

fails, halt execution.

Parameters:

  • other (Class)

    The other interaction.

  • inputs (Hash{Symbol => Object}) (defaults to: {})

    Attribute values to set.

Returns:

  • (Object)


# File 'lib/active_interaction/base.rb', line 209

#executeObject

This method is abstract.

Runs the business logic associated with the interaction. This method is only run when there are no validation errors. The return value is placed into Runnable#result. By default, this method is run in a transaction if ActiveRecord is available (see transaction).

Raises:

  • (NotImplementedError)


# File 'lib/active_interaction/base.rb', line 218

#inputsHash{Symbol => Object}

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

on the filters in the class.

Returns:

  • (Hash{Symbol => Object})

    All inputs passed to run or run!.

Since:

  • 1.0.0



232
233
234
235
236
# File 'lib/active_interaction/base.rb', line 232

def inputs
  self.class.filters.keys.each_with_object({}) do |name, h|
    h[name] = public_send(name)
  end
end