Class: ActiveInteraction::Base

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

rubocop:disable Lint/EmptyClass

Class Method Summary collapse

Instance Method Summary collapse

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.



163
164
165
166
167
168
169
# 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

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.



72
73
74
75
76
77
78
79
80
# 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

.filtersHash{Symbol => Filter}

Get all the filters defined on this interaction.

Returns:



85
86
87
88
89
# File 'lib/active_interaction/base.rb', line 85

def filters
  # rubocop:disable Naming/MemoizedInstanceVariableName
  @_interaction_filters ||= {}
  # rubocop:enable Naming/MemoizedInstanceVariableName
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:



124
125
126
127
128
129
130
131
132
133
# 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

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

  • input (Hash, ActionController::Parameters)

Returns:



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

.run!(inputs = {}) ⇒ Object

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

an exception if there were any validation errors.

Parameters:

  • input (Hash, ActionController::Parameters)

Returns:

  • (Object)

Raises:



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

Instance Method Details

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

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

fails, halt execution.

Parameters:

  • other (Class)

    The other interaction.

Returns:

  • (Object)


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

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

  • (NotImplementedError)


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

#inputsInputs

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

on the filters in the class.

Returns:



193
194
195
# File 'lib/active_interaction/base.rb', line 193

def inputs
  @_interaction_inputs
end

#read_attribute_for_validation(attribute) ⇒ Object



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

def read_attribute_for_validation(attribute)
  super(errors.local_attribute(attribute))
end