Class: ActiveInteraction::Base Abstract
- Inherits:
-
Object
- Object
- ActiveInteraction::Base
- 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/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
Subclass and override #execute to implement a custom ActiveInteraction::Base class.
Provides interaction functionality. Subclass this to create an interaction.
Class Method Summary collapse
-
.desc(desc = nil) ⇒ String?
Get or set the description.
-
.filters ⇒ Hash{Symbol => Filter}
Get all the filters defined on this interaction.
-
.import_filters(klass, options = {}) ⇒ Hash{Symbol => Filter}
Import filters from another interaction.
- .method_missing(*args, &block) ⇒ Object
-
.run(inputs = {}) ⇒ Base
Runs validations and if there are no errors it will call #execute.
- .run!(inputs = {}) ⇒ Object
Instance Method Summary collapse
-
#compose(other, inputs = {}) ⇒ Object
Run another interaction and return its result.
-
#execute ⇒ Object
abstract
Runs the business logic associated with the interaction.
-
#given?(input) ⇒ Boolean
Returns
trueif the given key was in the hash passed to Base.run. -
#initialize(inputs = {}) ⇒ Base
constructor
A new instance of Base.
- #inputs ⇒ Hash{Symbol => Object}
Methods included from Missable
Methods included from Hashable
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.
169 170 171 172 173 |
# File 'lib/active_interaction/base.rb', line 169 def initialize(inputs = {}) raise 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.
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/active_interaction/base.rb', line 76 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 |
.filters ⇒ Hash{Symbol => Filter}
Get all the filters defined on this interaction.
91 92 93 |
# File 'lib/active_interaction/base.rb', line 91 def filters @_interaction_filters ||= {} end |
.import_filters(klass, options = {}) ⇒ Hash{Symbol => Filter}
Import filters from another interaction.
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/active_interaction/base.rb', line 127 def import_filters(klass, = {}) only = [:only] except = [: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
96 97 98 99 100 101 102 |
# File 'lib/active_interaction/base.rb', line 96 def method_missing(*args, &block) super do |klass, names, | raise InvalidFilterError, 'missing attribute name' if names.empty? names.each { |name| add_filter(klass, name, , &block) } end end |
.run(inputs = {}) ⇒ Base
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.
|
|
# File 'lib/active_interaction/base.rb', line 43
|
Instance Method Details
#compose(other, inputs = {}) ⇒ Object
Run another interaction and return its result. If the other interaction
fails, halt execution.
|
|
# File 'lib/active_interaction/base.rb', line 175
|
#execute ⇒ Object
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.
|
|
# File 'lib/active_interaction/base.rb', line 184
|
#given?(input) ⇒ 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.
221 222 223 |
# File 'lib/active_interaction/base.rb', line 221 def given?(input) @_interaction_keys.include?(input.to_sym) end |
#inputs ⇒ Hash{Symbol => Object}
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 |