Class: ActiveInteraction::Base Abstract

Inherits:
Object
  • Object
show all
Extended by:
Hashable, Missable
Includes:
ActiveModelable, ActiveRecordable, 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/object_filter.rb,
lib/active_interaction/filters/record_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 ActiveRecordable

#column_for_attribute, #has_attribute?

Methods included from ActiveModelable

#i18n_scope, #new_record?, #persisted?

Constructor Details

#initialize(inputs = {}) ⇒ Base

Returns a new instance of Base.

Parameters:

  • (defaults to: {})

    Attribute values to set.

Since:

  • 1.0.0



170
171
172
173
# File 'lib/active_interaction/base.rb', line 170

def initialize(inputs = {})
  inputs = normalize_inputs!(inputs)
  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:

  • (defaults to: nil)

    What to set the description to.

Returns:

  • The description.

Since:

  • 1.0.0



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/active_interaction/base.rb', line 75

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



90
91
92
# File 'lib/active_interaction/base.rb', line 90

def filters
  @_interaction_filters ||= {}
end

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

Import filters from another interaction.

Parameters:

  • The other interaction.

  • (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



128
129
130
131
132
133
134
135
136
137
# File 'lib/active_interaction/base.rb', line 128

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



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

def method_missing(*args, &block) # rubocop:disable Style/MethodMissing
  super do |klass, names, options|
    raise 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:

  • (defaults to: {})

    Attribute values to set.

Returns:



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

.run!(inputs = {}) ⇒ Object

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

an exception if there were any validation errors.

Parameters:

  • (defaults to: {})

    Attribute values to set.

Returns:

Raises:

  • If there are validation errors.



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

Instance Method Details

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

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

fails, halt execution.

Parameters:

  • The other interaction.

  • (defaults to: {})

    Attribute values to set.

Returns:



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

#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.

Raises:



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

#given?(input, *rest) ⇒ Boolean

Returns ‘true` if the given key was in the hash passed to run. Otherwise returns `false`. Use this to figure out if an input was given, even if it was `nil`. Keys within nested hash filter can also be checked by passing them in series. Arrays can be checked in the same manor as hashes by passing an index.

rubocop:disable all

Examples:

class Example < ActiveInteraction::Base
  integer :x, default: nil
  def execute; given?(:x) end
end
Example.run!()        # => false
Example.run!(x: nil)  # => true
Example.run!(x: rand) # => true

Nested checks

class Example < ActiveInteraction::Base
  hash :x, default: {} do
    integer :y, default: nil
  end
  array :a, default: [] do
    integer
  end
  def execute; given?(:x, :y) || given?(:a, 2) end
end
Example.run!()               # => false
Example.run!(x: nil)         # => false
Example.run!(x: {})          # => false
Example.run!(x: { y: nil })  # => true
Example.run!(x: { y: rand }) # => true
Example.run!(a: [1, 2])      # => false
Example.run!(a: [1, 2, 3])   # => true

Parameters:

Returns:

Since:

  • 2.1.0



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/active_interaction/base.rb', line 242

def given?(input, *rest)
  filter_level = self.class
  input_level = @_interaction_inputs

  [input, *rest].each do |key_or_index|
    if key_or_index.is_a?(Symbol) || key_or_index.is_a?(String)
      key_or_index = key_or_index.to_sym
      filter_level = filter_level.filters[key_or_index]

      break false if filter_level.nil? || input_level.nil?
      break false unless input_level.key?(key_or_index) || input_level.key?(key_or_index.to_s)

      input_level = input_level[key_or_index] || input_level[key_or_index.to_s]
    else
      filter_level = filter_level.filters.first.last

      break false if filter_level.nil? || input_level.nil?
      break false unless key_or_index.between?(-input_level.size, input_level.size - 1)

      input_level = input_level[key_or_index]
    end
  end && true
end

#inputsHash{Symbol => Object}

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

on the filters in the class.

Returns:

  • All inputs passed to run or run!.

Since:

  • 1.0.0



197
198
199
200
201
# File 'lib/active_interaction/base.rb', line 197

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